3

我有以下代码在一个图中制作了四个子图:

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}}$")

结果如下所示:

如您所见,轴标签与刻度重叠。我想将公共轴标签从轴上移开一点。我无法弄清楚如何最好地做到这一点。

4

1 回答 1

3

使用labelpad参数set_ylabelset_xlabel方法:

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 中,但设置它的方式可能不同(我不确定)。
在网上,我发现了一些指向这种用法的评论:

ax.xaxis.LABELPAD = 8  # default is 5

我也见过这样的:

ax.xaxis.labelpad = 8
于 2012-12-31T11:17:29.177 回答