-5

I'm trying to plot a dataset about the exchange rate per Euro, all the codes are working perfectly, except the below code, I don't mind to change the code to something easier to read

style.use('fivethirtyeight')

plt.figure(figsize=(12, 6))
ax1 = plt.subplot(2,3,1)
ax2 = plt.subplot(2,3,2)
ax3 = plt.subplot(2,3,3)
ax4 = plt.subplot(2,1,2)

axes = [ax1, ax2, ax3, ax4]

for ax in axes:
    ax.set_ylim(0.8, 1.7)
    ax.set_yticks([1.0, 1.2, 1.4, 1.6])
    ax.set_yticklabels(['1.0', '1.2','1.4', '1.6'],
                   alpha=0.3)
    ax.grid(alpha=0.5)    

ax1.plot(bush['Time'], bush['rolling_mean'],
         color='#BF5FFF')
ax1.text(731516.0, 1.92, 'BUSH', fontsize=18, weight='bold',
        color='#BF5FFF')
ax1.text(731216.0, 1.8, '2001-2009', 
        weight='bold', alpha=0.3)
ax1.set_xticklabels(['', '2001', '', '2003', '', '2005', '',
                     '2007', '', '2009'],
                   alpha=0.3)


ax2.plot(obama['Time'], obama['rolling_mean'],
         color='#ffa500')
ax2.text(734288.0, 1.92, 'OBAMA', fontsize=18, weight='bold',
        color='#ffa500')
ax2.text(734138.0, 1.8, '2009-2017', 
        weight='bold', alpha=0.3)
ax2.set_xticklabels(['', '2009', '', '2011', '', '2013', '',
                     '2015', '', '2017'],
                   alpha=0.3)

# visualising Trumps's period

ax3.plot(trump['Time'], trump['rolling_mean'],
         color='#00B2EE')
ax3.text(736855.0, 1.92, 'TRUMP', fontsize=18, weight='bold',
        color='#00B2EE')
ax3.text(736745.0, 1.8, '2009-2017', 
        weight='bold', alpha=0.3)
ax3.set_xticklabels(['2017', '', '2018', '', '2019', '',
                     '2020', '', '2021'],
                   alpha=0.3)

ax4.plot(bush['Time'], bush['rolling_mean'],
        color='#BF5FFF')
ax4.plot(obama['Time'], obama['rolling_mean'],
        color='#ffa500')
ax4.plot(trump['Time'], trump['rolling_mean'],
        color='#00B2EE')
ax4.grid(alpha=0.5)
ax4.set_xticks([])

ax1.text(730016.0, 2.35, 'EURO-USD rate averaged 1.22 under the last three US presidents',
         fontsize=20, weight='bold')
ax1.text(730016.0, 2.14, '''EURO-USD exchange rates under George W. Bush (2001 - 2009), Barack Obama (2009-2017),
and Donald Trump (2017-2021)''',
        fontsize=16)

ax4.text(729916.0, 0.65, '©MAHA FOUAD' + ' '*103 + 'Source: European Central Bank',
        color = '#f0f0f0', backgroundcolor = '#4d4d4d',
        size=14)

plt.show()

I was expecting it to work perfectly, but I get the below error:

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

  • Based on the error message we can see that it is trying to generate a plot image that is too wide. Try rendering only one subplot at a time, or changing labels/fonts/data/etc until you find what component is causing your X axis to be so large. – 0x5453 Mar 06 '23 at 16:26
  • 1
    Welcome to Stack Overflow! Please take the [tour]. SO is a Q&A site, but this isn't a question, and it's missing necessary details. For help with code, you need to make a [mre] included in the question and clarify what exactly you need help with. Like, to start, do you understand what the error message means? You can [edit]. – wjandrea Mar 06 '23 at 16:29
  • You don't say but presumably you are using a jupyter notebook and the inline backend. some of your data is wildly off the axes, and the implicit savefig(..., bbox_inches="tight") is making a gigantic image. My guess is that numbers like 730016.0 are far too large, because you have mixed epochs. See https://matplotlib.org/stable/gallery/ticks/date_precision_and_epochs.html – Jody Klymak Mar 06 '23 at 17:59
  • This question is not reproducible without **data**. This question needs a [SSCCE](http://sscce.org/). Please see [How to provide a reproducible dataframe](https://stackoverflow.com/q/52413246/7758804), then **[edit] your question**, and paste the clipboard into a code block. Always provide a [mre] **with code, data, errors, current output, and expected output, as [formatted text](https://stackoverflow.com/help/formatting)**. If relevant, plot images are okay. If you don't include an mre, it is likely the question will be downvoted, closed, and deleted. – Trenton McKinney Mar 06 '23 at 18:00

1 Answers1

0

You don't have reproducible data, but probably this is caused by

ax1.text(730016.0, 2.35, 'EURO-USD rate averaged 1.22 under the last three US presidents',
         fontsize=20, weight='bold')

being too far off axis, and you are using a jupyter notebook or similar and bbox_inches='tight' causing the data to be off axes.

I would suggest just putting a datetime object in ax.text:


ax1.text(np.datetime64('2002-04-01'), 2.35, 'EURO-USD rate averaged 1.22 under the last three US presidents',
         fontsize=20, weight='bold')
Jody Klymak
  • 4,979
  • 2
  • 15
  • 31