You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

绘制欧元汇率图表遇ValueError:图像尺寸过大问题求助

解决Matplotlib绘制欧元兑美元汇率时的图片尺寸过大错误

执行你的Matplotlib代码时触发ValueError: Image size of 99792x523 pixels is too large. It must be less than 2^16 in each direction,核心原因是你使用了原始的Matplotlib日期编码值(如731516.0)作为文本的x坐标,这些数值远超图表实际的x轴范围,导致Matplotlib错误地将画布拉伸到异常尺寸。

以下是修复并优化可读性后的完整代码,同时修正了几处细节错误:

import matplotlib.pyplot as plt
from matplotlib import style

style.use('fivethirtyeight')

# 创建画布与子图,调整布局避免重叠
plt.figure(figsize=(12, 8))
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')
# 用轴范围相对定位文本,避免硬编码日期值
x_min, x_max = ax1.get_xlim()
ax1.text(x_max * 0.85, 1.92, 'BUSH', fontsize=18, weight='bold', color='#BF5FFF')
ax1.text(x_max * 0.75, 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')
x_min, x_max = ax2.get_xlim()
ax2.text(x_max * 0.85, 1.92, 'OBAMA', fontsize=18, weight='bold', color='#ffa500')
ax2.text(x_max * 0.75, 1.8, '2009-2017', weight='bold', alpha=0.3)
ax2.set_xticklabels(['', '2009', '', '2011', '', '2013', '', '2015', '', '2017'], alpha=0.3)

# 绘制特朗普时期汇率(修正年份错误)
ax3.plot(trump['Time'], trump['rolling_mean'], color='#00B2EE')
x_min, x_max = ax3.get_xlim()
ax3.text(x_max * 0.85, 1.92, 'TRUMP', fontsize=18, weight='bold', color='#00B2EE')
ax3.text(x_max * 0.75, 1.8, '2017-2021', 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的轴范围定位
x_min, x_max = ax1.get_xlim()
ax1.text(x_min, 2.35, 'EURO-USD rate averaged 1.22 under the last three US presidents',
         fontsize=20, weight='bold')
ax1.text(x_min, 2.14, '''EURO-USD exchange rates under George W. Bush (2001 - 2009), Barack Obama (2009-2017),
and Donald Trump (2017-2021)''', fontsize=16)

# 添加底部版权文本,用ax4的轴范围定位
x_min, x_max = ax4.get_xlim()
ax4.text(x_min, 0.65, '©MAHA FOUAD' + ' '*103 + 'Source: European Central Bank',
        color='#f0f0f0', backgroundcolor='#4d4d4d', size=14)

# 自动调整子图间距,避免重叠
plt.tight_layout()
plt.show()

关键修复与优化点:

  • 替换硬编码日期坐标:用ax.get_xlim()获取当前轴的x范围,通过相对比例定位文本,彻底避免因超大日期值导致的画布尺寸异常
  • 修正年份错误:特朗普时期的文本年份从2009-2017改为正确的2017-2021
  • 优化布局:添加plt.tight_layout()自动调整子图间距,避免文本和图表元素重叠
  • 统一逻辑:将轴属性设置、文本定位的逻辑统一,提升代码可读性与可维护性

内容的提问来源于stack exchange,提问作者Maha Fouad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 05:40:29