我有以下代码,可以在一张图中制作四个子图:
f = figure( figsize=(7,7) )
f.add_axes([0.2,0.175,0.75,0.75])
f.subplots_adjust(left=0.15)
f.clf()
ax = f.add_subplot(111)
ax1 = f.add_subplot(221)
ax2 = f.add_subplot(222)
ax3 = f.add_subplot(223)
ax4 = f.add_subplot(224)
ax.xaxis.set_major_formatter( NullFormatter() )
ax.yaxis.set_major_formatter( NullFormatter() )
ax2.xaxis.set_major_formatter( NullFormatter() )
ax2.yaxis.set_major_formatter( NullFormatter() )
ax1.xaxis.set_major_formatter( NullFormatter() )
ax4.yaxis.set_major_formatter( NullFormatter() )
f.subplots_adjust(wspace=0,hspace=0)
ax1.plot(tbins[0:24], mean_yszth1, color='r', label='mean', marker='.', lw=3)
ax2.plot(tbins[0:24], mean_ysz1, color='r', label='mean', marker='.', lw=3)
ax3.plot(tbins[0:24], mean_yszth2, color='r', label='mean', marker='.', lw=3)
ax4.plot(tbins[0:24], mean_ysz2, color='r', label='mean', marker='.', lw=3)
ax1.set_xlim(0,12)
ax1.set_ylim(-0.5,0.5)
ax2.set_xlim(0,12)
ax2.set_ylim(-0.5,0.5)
ax3.set_xlim(0,12)
ax3.set_ylim(-0.5,0.5)
ax4.set_xlim(0,12)
ax4.set_ylim(-0.5,0.5)
ax.set_xlabel(r"$\mathrm{Time\ since\ last\ merger\ (Gyr)}$")
ax.set_ylabel(r"$\mathrm{\Delta Y_{SZ}/Y_{SZ}}$")
结果是这样的:
如您所见,轴标签与刻度重叠。我想将公共(public)轴标签从轴上移开一点。我不知道如何最好地做到这一点。
最佳答案
使用 set_ylabel
和 set_xlabel
方法的 labelpad
参数:
Definition: ax.set_ylabel(self, ylabel, fontdict=None, labelpad=None, **kwargs)
Docstring:
Call signature::
set_ylabel(ylabel, fontdict=None, labelpad=None, **kwargs)
Set the label for the yaxis
*labelpad* is the spacing in points between the label and the y-axis
这是我将 labelpad 设置为 50 (x) 和 60 (y) 时得到的结果。使用默认配置时,由于标签在图形框架之外,我不得不手动修改图形边距。
编辑
从您的评论来看,您似乎使用的是非常旧版本的 matplotlib。 Labelpad 参数在很多版本以前的 matplotlib 中都有,但设置它的方式可能不同(我不确定)。
在网上我找到了some comments指向这种用法:
ax.xaxis.LABELPAD = 8 # default is 5
我也见过这样的:
ax.xaxis.labelpad = 8
关于Python 子图为公共(public)轴标签留出空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14099435/