Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib error 'Image size of 362976x273 pixels is too large'

Tags:

matplotlib

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?

like image 967
W_YY Avatar asked Sep 17 '18 20:09

W_YY


1 Answers

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)
like image 143
ImportanceOfBeingErnest Avatar answered Oct 26 '22 10:10

ImportanceOfBeingErnest