gpt4 book ai didi

python - 为什么在使用 matplotlib 的 DateFormatter 格式化 x 轴上的日期时会出现 "python int too large to convert to C long"错误?

转载 作者:太空狗 更新时间:2023-10-29 22:15:52 31 4
gpt4 key购买 nike

正在关注 this answer's use of DateFormatter ,我尝试使用 pandas 0.15.0 和 matplotlib 1.4.2 绘制时间序列并用年份标记其 x 轴:

import datetime as dt
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas.io.data as pdio
import scipy as sp

t1 = dt.datetime(1960, 1, 1)
t2 = dt.datetime(2014, 6, 1)
data = pdio.DataReader("GS10", "fred", t1, t2).resample("Q", how=sp.mean)

fig, ax1 = plt.subplots()
ax1.plot(data.index, data.GS10)
ax1.set_xlabel("Year")
ax1.set_ylabel("Rate (%)")
ax1.xaxis.set_major_formatter(mpl.dates.DateFormatter("%Y"))
fig.suptitle("10-yr Treasury Rate", fontsize=14)

fig.savefig('test.eps')

最后一行抛出错误:OverflowError: Python int too large to convert to C long
使用此回溯:

C:\Anaconda3\lib\site-packages\IPython\core\formatters.py:239: FormatterWarning: Exception in image/png formatter: Python int too large to convert to C long FormatterWarning, Traceback (most recent call last):

File "", line 1, in runfile('D:/username/latex_template/new_pandas_example.py', wdir='D:/username/latex_template')

File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 580, in runfile execfile(filename, namespace)

File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 48, in execfile exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)

File "D:/username/latex_template/new_pandas_example.py", line 18, in fig.savefig('test.eps')

File "C:\Anaconda3\lib\site-packages\matplotlib\figure.py", line 1470, in savefig self.canvas.print_figure(*args, **kwargs)

File "C:\Anaconda3\lib\site-packages\matplotlib\backend_bases.py", line 2194, in print_figure **kwargs)

File "C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_ps.py", line 992, in print_eps return self._print_ps(outfile, 'eps', *args, **kwargs)

File "C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_ps.py", line 1020, in _print_ps **kwargs)

File "C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_ps.py", line 1110, in _print_figure self.figure.draw(renderer)

File "C:\Anaconda3\lib\site-packages\matplotlib\artist.py", line 59, in draw_wrapper draw(artist, renderer, *args, **kwargs)

File "C:\Anaconda3\lib\site-packages\matplotlib\figure.py", line 1079, in draw func(*args)

File "C:\Anaconda3\lib\site-packages\matplotlib\artist.py", line 59, in draw_wrapper draw(artist, renderer, *args, **kwargs)

File "C:\Anaconda3\lib\site-packages\matplotlib\axes_base.py", line 2092, in draw a.draw(renderer)

File "C:\Anaconda3\lib\site-packages\matplotlib\artist.py", line 59, in draw_wrapper draw(artist, renderer, *args, **kwargs)

File "C:\Anaconda3\lib\site-packages\matplotlib\axis.py", line 1114, in draw ticks_to_draw = self._update_ticks(renderer)

File "C:\Anaconda3\lib\site-packages\matplotlib\axis.py", line 957, in _update_ticks tick_tups = [t for t in self.iter_ticks()]

File "C:\Anaconda3\lib\site-packages\matplotlib\axis.py", line 957, in tick_tups = [t for t in self.iter_ticks()]

File "C:\Anaconda3\lib\site-packages\matplotlib\axis.py", line 905, in iter_ticks for i, val in enumerate(majorLocs)]

File "C:\Anaconda3\lib\site-packages\matplotlib\axis.py", line 905, in for i, val in enumerate(majorLocs)]

File "C:\Anaconda3\lib\site-packages\matplotlib\dates.py", line 411, in call dt = num2date(x, self.tz)

File "C:\Anaconda3\lib\site-packages\matplotlib\dates.py", line 345, in num2date return _from_ordinalf(x, tz)

File "C:\Anaconda3\lib\site-packages\matplotlib\dates.py", line 225, in _from_ordinalf dt = datetime.datetime.fromordinal(ix)

OverflowError: Python int too large to convert to C long

我在这里使用 DateFormatter 不正确吗?我如何轻松地将年份(或任何时间格式,因为我的时间序列可能不同)放在 matplotlib 图的 a 轴上?

最佳答案

这是 pandas 0.15 中的“回归”(由于索引的重构),参见 https://github.com/matplotlib/matplotlib/issues/3727https://github.com/pydata/pandas/issues/8614 ,但已在 0.15.1 中修复


短篇小说:matplotlib 现在将 pandas 索引视为 datetime64[ns] 值的数组(实际上是非常大的 int64),而不是时间戳数组(它们是 datetime 的子类)。 datetime,并且可以由 matplotlib 处理)在以前版本的 pandas 中。所以根本原因是 matplotlib 不将 datetime64 作为日期值处理,而是作为整数处理。

对于 pandas 0.15.0(但最好升级到更新的版本),有两种可能的解决方法:

  • 注册 datetime64 类型,因此它也将被 matplotlib 处理为日期:

    units.registry[np.datetime64] = pd.tseries.converter.DatetimeConverter()
  • 或者使用 to_pydatetime 方法将 DatetimeIndex(具有 datetime64 值)转换为 datetime.datetime 值的数组,然后绘制:

    ax1.plot(data.index.to_pydatetime(), data.GS10)

相关问题:Plotting datetimeindex on x-axis with matplotlib creates wrong ticks in pandas 0.15 in contrast to 0.14

关于python - 为什么在使用 matplotlib 的 DateFormatter 格式化 x 轴上的日期时会出现 "python int too large to convert to C long"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26642133/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com