我正在使用Matplotlib,尝试调整绘制包含子图网格的图形周围的空白区域。
要求:能够选择所有子图的纵横比(它们都是相同的),定义子图与保存的图形边缘之间有多少空白区域,包括顶部、底部和左侧。 我不需要右侧有任何额外的空间。
到目前为止,我已经编写了以下代码:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(ncols=6, nrows=2, sharex=True,
sharey=True, figsize=(16,8))
axes[0,0].plot(x,y)
etc...
plt.subplots_adjust(hspace=1.0, wspace=0.02, bottom=0.17, left=0.075, top=0.18)
plt.savefig('Fig1_matplot.pdf', bbox_inches='tight')
当前情况下,如果我不指定figsize=()
,子图的纵横比就会出错。如果我这样做,那么每当我在subplots_adjust()
中更改参数时,它的纵横比就会发生变化。显然,left
不能 >= right
,bottom
也不能 >= top
。如果我定义的top
不是0,则它会将所有的子图完全压缩成位于图的底部的一条线。
如果我指定bbox_inches='tight'
,则它会切掉我定义的底部和左侧的空白,但如果我没有指定它,那么右侧就会留下多余的空白,因为我无法摆脱它(right
小于等于 left
)。
我有些困惑,请提供任何帮助将不胜感激。
谢谢。
adjust
和legend_loc
,并传递adjust={'left': 0.125, 'bottom': 0.1, 'right': 0.9, 'top': 0.9}
(典型的默认值),例如legend_loc=(1.0, 1.3)
。在方便函数内部,我有plt.subplots_adjust(left=adjust['left'], right=adjust['right'], bottom=adjust['bottom'], top=adjust['top'])
,紧接着是plt.savefig
和ax.legend(bbox_to_anchor=legend_loc)
,对于单个轴ax
。 - PatrickT