Matplotlib在图中填充白色空间

时间:2016-05-19 18:21:41

标签: python matplotlib

我在matplotlib中创建了一个共享轴图,如下所示:

enter image description here

我尝试了很多在X轴的Jan和Dec附近填充一点白色空间,使其看起来更具美感,但无法找到任何方法。任何人都可以对此提出一些建议吗?

我的代码:

fig, axes1 = plt.subplots(figsize=(12,8))

axes1.plot(misr_aod, label='MISR', color='blue', linestyle=':', linewidth=2, marker='+')
axes1.plot(modis_aod, label='MODIS', color='green', linestyle='-.', linewidth=2, marker='o')
axes1.plot(CESM_aod, label='CESM_AOD', color='red', linestyle='--', linewidth=2, marker='s')
axes1.set_ylim([0,0.8])
axes1.legend(loc=2)
axes1.set_xticks([-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
axes1.set_xticklabels(['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', ''])
axes1.grid(True)
axes1.set_xlabel('Months')
axes1.set_ylabel('AOD at 500 nm')


axes2 = axes1.twinx()

axes2.plot(APHRO_pcp, label='APHRO', color="magenta", linewidth=2, markerfacecolor="yellow", markeredgewidth=2, markeredgecolor="blue")
axes2.plot(IMD_pcp, label='IMD', color="purple", linewidth=2)
axes2.plot(TRMM_pcp, label='TRMM', color="green", linewidth=2)
axes2.plot(CESM_pcp, label='CESM_PRECT', color="red", linewidth=2)
axes2.set_ylim([0,10])
yticks = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
axes2.set_yticks(yticks)
axes2.set_ylabel('PRECIPITATION (mm/day)')
axes2.legend()
plt.show()

1 个答案:

答案 0 :(得分:1)

您可以使用ax.margins()在x轴上的数据之前和之后自动填充一些空格。

对于您的脚本,您只需要:

axes1.margins(x=0.05)

在两端添加5%的x范围。显然,您只需要调整x以满足您的需求。