【问题标题】:matplotlib error 'Image size of 362976x273 pixels is too large'matplotlib 错误“362976x273 像素的图像尺寸太大”
【发布时间】:2018-09-18 06:33:37
【问题描述】:

我正在绘制一个带有折线图的面积图作为数据框结构顶部的衬里,如下所示:

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),不工作。有谁知道问题出在哪里?

【问题讨论】:

标签: matplotlib


【解决方案1】:

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

问题来自plt.text 行。坐标轴范围在 2018 年的范围内,因此数值单位在 17500 左右。然而,文本位于位置 1。那是2017年前的事了。

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

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

或者您想将其定位在轴单位中,例如

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

【讨论】:

  • 我取出 plt.text 位,错误消失了。所以这正是造成麻烦。谢谢!
  • @ImportanceOfBeingErnest 您是如何达到 17500 的?那是从 1970 年开始的日子吗?我无法让它发挥作用。我在 transform 参数上取得了更大的成功。
  • @Maddenker Pandas 动态选择日期时间单位。您可以使用ax.get_xlim() 了解选择了哪些单位。
猜你喜欢
  • 1970-01-01
  • 2020-12-25
  • 2019-02-14
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-28
相关资源
最近更新 更多