Python子图为公共轴标签留出空间 [英] Python subplots leaving space for common axis labels

查看:190
本文介绍了Python子图为公共轴标签留出空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码可以在一幅图中划分四个子图:

I have the following code that makes four subplots in one figure:

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

结果看起来像这样:

The result looks like this:

如您所见,轴标签与刻度线重叠.我想将公共轴标签稍微远离轴.我不知道如何做到最好.

As you can see, the axis labels overlap with the ticks. I would like to move the common axis labels away from the axes a little. I can't figure out how best to do this.

推荐答案

使用set_ylabelset_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)的结果.使用默认配置时,标签必须在图形框之外,因此我不得不手动修改图形边距.

This is what I get with labelpad set to 50 (x) and 60 (y). I had to modify manually figure margins as the labels were outside the figure frame when using the default configuration.

修改
从您的评论看来,您可能正在使用过旧的matplotlib版本. Labelpad参数从很多版本开始就已经存在于matplotlib中,但是设置它的方式可能有所不同(我不确定).
在网络上,我发现了一些评论指出这种用法:

Edit
From your comments it seems you could be using a very old version of matplotlib. Labelpad parameter has been in matplotlib from many versions ago but the way to of setting it could be different (I do not know for sure).
In the web I found some comments that point to this usage:

ax.xaxis.LABELPAD = 8  # default is 5

我也看到过这样的话:

ax.xaxis.labelpad = 8

这篇关于Python子图为公共轴标签留出空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆