如何在不更改绘图的情况下更改matplotlib中的图形画布大小?

mvd*_*mvd 1 python matplotlib

我想用matplotlib中的面板标签("A","B",......")来标注我正在制作的图表.例如,我想要注释一组带有大"A"的子图.图中的左上角如下面的代码所示.如何更改图形画布以使此"A"可见?

f = plt.figure(figsize=(5,5))
plt.subplot(2, 2, 1)
# this "A" is clipped in the plot
plt.annotate("A",
             xy = (-0.25, 1.1),
             xytext = (-0.25, 1.1),
             xycoords = 'axes fraction',
             textcoords = 'axes fraction',
             fontsize=30)
plt.subplot(2, 2, 2)
plt.subplot(2, 2, 3)
plt.subplot(2, 2, 4)
plt.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)

如何通过增加子图周围的填充来为文本标签腾出空间?

有没有办法在子图的左侧添加"填充",以便将它们推到图的右侧,而不更改figsize参数?并没有重新调整情节?画布上有足够的空间来贴合标签和子图.

Nei*_*bst 5

添加plt.subplots_adjust():

f = plt.figure(figsize=(5,5))
plt.subplot(2, 2, 1)
# this "A" is clipped in the plot
plt.annotate("A",
             xy = (-0.25, 1.1),
             xytext = (-0.25, 1.1),
             xycoords = 'axes fraction',
             textcoords = 'axes fraction',
             fontsize=30)
plt.subplot(2, 2, 2)
plt.subplot(2, 2, 3)
plt.subplot(2, 2, 4)
plt.tight_layout()
plt.subplots_adjust(top=0.9)
plt.show()
Run Code Online (Sandbox Code Playgroud)

只是确保不要放在plt.tight_layout后面,plt.subplots_adjust因为这将覆盖更改.

另请参阅页面调整子图.

使用上面代码生成的数字如下所示: 在此输入图像描述