matplotlib 我得到值错误:99792x523像素的图像大小太大,每个方向的图像大小必须小于2^16

bvjxkvbb  于 2023-03-09  发布在  其他
关注(0)|答案(1)|浏览(108)

我正在尝试绘制一个关于欧元汇率的数据集,所有代码都工作得很好,除了下面的代码,我不介意将代码更改为更容易阅读的代码

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

我期待它能完美地工作,但我得到下面的错误:
ValueError:99792x523像素的图像大小太大。在每个方向上必须小于2^16。

vnzz0bqm

vnzz0bqm1#

您没有可复制的数据,但这可能是由于

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

离轴太远,而且你正在使用Jupyter笔记本或类似的和bbox_inches='tight'导致数据离轴。
我建议只在ax.text中放置一个datetime对象:

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

相关问题