Matplotlib 错误'Image size of 362976x273 pixels is too large'



我正在绘制一个面积图,其中折线图作为数据帧结构的衬里,如下所示:

df = pd.DataFrame({'Date':pd.date_range('2018-1-1', periods=100, freq='D'),'Pre1':np.random.randint(-1000, 1000, 100),
'Pre2':np.random.randint(-750, 1000, 100)},
columns=['Date','Pre1','Pre2'])
df=df.set_index('Date')

DatetimeIndex(['2017-01-01', '2017-01-03', '2017-01-04', '2017-01-05',
'2017-01-06', '2017-01-09', '2017-01-10', '2017-01-11',
'2017-01-12', '2017-01-13',
...
'2018-08-29', '2018-08-30', '2018-08-31', '2018-09-03',
'2018-09-04', '2018-09-05', '2018-09-06', '2018-09-07',
'2018-09-10', '2018-09-11'],
dtype='datetime64[ns]', name='Date', length=416, freq=None)

我正在使用

plt.figure()
ax3=df.plot(df.index,'Pre1', color="g",linewidth=0.8) #the line
plt.fill_between(df.index,df['Pre1'], facecolor="palegreen", alpha=0.4) # the area
ax3.tick_params(axis="x",direction="in")
ax3.tick_params(axis="y",direction="in")
plt.text(1,100,'Pre')
plt.text(3,-100,'Dis')
plt.ylabel('$')
ax3.legend().set_visible(False)
plt.title('This is a test')
plt.xticks()
ax3.yaxis.grid(True,linestyle='--')
plt.show()

但是,我得到以下错误:

ValueError: Image size of 362976x273 pixels is too large. It must be less than 2^16 in each direction.

我试图重新启动内核和Jupyter,但没有运气。还尝试了 figsize=(6,8(,不起作用。有谁知道问题是什么?

我认为应该是ax3=df.plot(y='Pre1', color="g",linewidth=0.8).但这可能与错误无关。

问题来自plt.text线。轴限制在 2018 年的范围内,因此以 17500 左右的数字单位表示。但是,文本被放在位置1。那是2017年前的2017年。

对于某些(未知或尚未确定的原因(,当此代码在 jupyter 中运行时,文本仍将是轴的一部分,并且不会被剪裁。这有效地使轴2017年长,并最终产生一个巨大的数字。人们可能会认为这是一个错误。但即便如此,您可能也不想将文本放置在可以看到的范围之外。因此,您希望将文本放置在可见范围内的某些数据坐标处,例如

plt.text(17527,100,'Pre')

或者你想以轴单位定位它,比如

plt.text(1,1,'Pre', transform=ax3.transAxes)

相关内容

最新更新