首页 文章

当我使用matplotlib的DateFormatter格式化x轴上的日期时,为什么我的“python int太大而无法转换为C long”错误?

提问于
浏览
6

this answer's use of DateFormatter之后,我尝试使用pandas 0.15.0和matplotlib 1.4.2绘制时间序列并用x年标记其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:image / png格式化程序中的异常:Python int太大而无法转换为C long FormatterWarning,Traceback(最近一次调用最后一次):文件“”,第1行,在runfile中('D:/username/latex_template/new_pandas_example.py',wdir ='D:/ username / latex_template')文件“C:\ Anaconda3 \ lib \ site-packages \ spyderlib \ widgets \ externalshell \ sitecustomize.py“,第580行,在runfile execfile(文件名,命名空间)文件”C:\ Anaconda3 \ lib \ site-packages \ spyderlib \ widgets \ externalshell \ sitecustomize.py“,第48行,在execfile exec中( compile(open(filename,'rb') . read(),filename,'exec'),namespace)文件“D:/username/latex_template/new_pandas_example.py”,第18行,在fig.savefig中('test.eps ')文件“C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ figure.py”,第1470行,在savefig中self.canvas.print_figure(* args,** kwargs)文件“C:\ Anaconda3 \ lib \ site -packages \ matplotlib \ backend_bases.py“,第2194行,在print_figure ** kwargs中)文件”C:\ Anaconda3 \ lib \ site-pack age \ matplotlib \ backends \ backend_ps.py“,第992行,在print_eps中返回self._print_ps(outfile,'eps',* args,** kwargs)文件”C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ backends \ backend_ps.py“,第1020行,在_print_ps ** kwargs中)文件”C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ backends \ backend_ps.py“,第1110行,在_print_figure self.figure.draw(renderer)文件“C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ artist.py”,第59行,在draw_wrapper中绘制(艺术家,渲染器,* args,** kwargs)文件“C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ figure.py“,第1079行,在draw func(* args)文件中”C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ artist.py“,第59行,在draw_wrapper中绘制(艺术家,渲染器,* args,** kwargs)文件“C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ axes_base.py”,第2092行,在绘制a.draw(渲染器)文件“C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ artist.py“,第59行,在draw_wrapper中绘制(艺术家,渲染器,* args,** kwargs)文件”C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ axis.py“,第1114行,正在绘制蜱s_to_draw = self._update_ticks(renderer)文件“C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ axis.py”,第957行,在_update_ticks中tick_tups = [t for self in self.iter_ticks()]文件“C: \ Anaconda3 \ lib \ site-packages \ matplotlib \ axis.py“,第957行,在tick_tups = [t for self in self.iter_ticks()]文件”C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ axis . py“,第905行,in iter_ticks for i,val in enumerate(majorLocs)]文件”C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ axis.py“,第905行,in for i,val in enumerate(majorLocs) )]文件“C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ dates.py”,第411行,在调用dt = num2date(x,self.tz)文件“C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ dates.py“,第345行,在num2date中返回_from_ordinalf(x,tz)文件”C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ dates.py“,第225行,在_from_ordinalf dt = datetime.datetime中 . fromordinal(ix)OverflowError:Python int太大而无法转换为C long

我在这里错误地使用了 DateFormatter 吗?我怎样才能轻松地在matplotlib图的a轴上放置年(或任何时间格式,因为我的时间序列可能不同)?

1 回答

  • 11

    这是pandas 0.15中的'regression'(由于Index的重构),请参见https://github.com/matplotlib/matplotlib/issues/3727https://github.com/pydata/pandas/issues/8614,但是 is fixed in 0.15.1 .


    简短的说明:matplotlib现在将pandas索引视为一个 datetime64[ns] 值的数组(实际上是非常大的int64s),而不是以前版本中的Timestamps数组(它们是datetime.datetime的子类,可以由matplotlib处理)大熊猫 . 因此,潜在的原因是matplotlib不会将datetime64作为日期值处理,而是作为整数处理 .

    对于pandas 0.15.0(但更好地升级到更新版本),有两种可能 workarounds

    • 注册 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

相关问题