16

I am plotting a area chart with a line plot as lining on top of a dataframe structure like below:

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')

and

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)

I am using

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()

However, I got below error :

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

I have tried to restart kernel as well as Jupyter but with no luck. Also tried figsize=(6,8), not working. Does anyone knows what the problem is?

W_YY
  • 189
  • 1
  • 1
  • 8
  • Does this answer your question? [Image size of 1005x132589 pixels is too large. It must be less than 2^16 in each direction](https://stackoverflow.com/questions/51980366/image-size-of-1005x132589-pixels-is-too-large-it-must-be-less-than-216-in-each) – jtlz2 Jan 25 '21 at 11:16

1 Answers1

22

I think it should be ax3=df.plot(y='Pre1', color="g",linewidth=0.8). But that might be independent of the error.

The problem comes from the plt.text lines. The axis limits are in the range of the year 2018, hence in numerical units somewhere around 17500. However the text is put at position 1. That is 2017 years earlier.

For some (unknown or yet to be determined reason) the text will still be part of the axes and not be clipped when this code is run in jupyter. This effectively makes the axes 2017 years long and eventually produce a huge figure. One may consider this to be a bug. But even so, you probably don't want to place your text outside the range where it can be seen. Hence you would either want to place your text at some datacoordinates within the visible range, like

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

Or you want to position it in axes units, like

plt.text(1,1,'Pre', transform=ax3.transAxes)
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • 1
    I took out plt.text bit and the error gone. So that's is exactly causing trouble. Thanks! – W_YY Sep 19 '18 at 14:28
  • 1
    @ImportanceOfBeingErnest How did you get to 17500? Is that days since 1970? I could not get that to work. I had much more success with the transform parameter. – Maddenker Nov 04 '19 at 21:09
  • 2
    @Maddenker Pandas choses its units for datetimes on the fly. You can use `ax.get_xlim()` to know which units are chosen. – ImportanceOfBeingErnest Nov 04 '19 at 21:35