用子图更新了MRE
matplotlib 3.4.2
可重现此问题。
fig, axes = plt.subplots(ncols=2, nrows=2, figsize=(8, 6))
axes = axes.flatten()
for ax in axes:
ax.set_ylabel(r'$\ln\left(\frac{x_a-x_b}{x_a-x_c}\right)$')
ax.set_xlabel(r'$\ln\left(\frac{x_a-x_d}{x_a-x_e}\right)$')
plt.show()
原创
我正在使用matplotlib
绘制一个数据集,其中我有一个相当“高”的xlabel (这是一个用TeX呈现的公式,它包含一个分数,因此它的高度相当于几行文本)。
在任何情况下,当我画图形时,公式的底部总是被切掉。更改图形大小似乎对此无济于事,而且我还不知道如何将x轴“上移”来为xlabel腾出空间。像这样的东西将是一个合理的临时解决方案,但更好的是有一种方法可以让matplotlib自动识别标签被截断并相应地调整大小。
下面是我的意思的一个例子:
import matplotlib.pyplot as plt
plt.figure()
plt.ylabel(r'$\ln\left(\frac{x_a-x_b}{x_a-x_c}\right)$')
plt.xlabel(r'$\ln\left(\frac{x_a-x_d}{x_a-x_e}\right)$', fontsize=50)
plt.title('Example with matplotlib 3.4.2\nMRE no longer an issue')
plt.show()
整个ylabel是可见的,但是xlabel在底部被截断了。
在这种情况下,这是一个特定于计算机的问题,我使用matplotlib 1.0.0在OSX 10.6.8上运行
发布于 2011-07-21 21:11:18
使用:
import matplotlib.pyplot as plt
plt.gcf().subplots_adjust(bottom=0.15)
# alternate option without .gcf
plt.subplots_adjust(bottom=0.15)
为标签腾出空间,其中plt.gcf()
表示,获取当前图形。也可以使用获取当前Axes
的plt.gca()
。
编辑:
因为我给出了答案,所以matplotlib
添加了函数。
See matplotlib Tutorials: Tight Layout Guide
所以我建议使用它:
fig, axes = plt.subplots(ncols=2, nrows=2, figsize=(8, 6))
axes = axes.flatten()
for ax in axes:
ax.set_ylabel(r'$\ln\left(\frac{x_a-x_b}{x_a-x_c}\right)$')
ax.set_xlabel(r'$\ln\left(\frac{x_a-x_d}{x_a-x_e}\right)$')
plt.tight_layout()
plt.show()
发布于 2018-10-01 16:57:45
如果您想要将其存储到文件中,可以使用bbox_inches="tight"
参数解决它:
plt.savefig('myfile.png', bbox_inches="tight")
发布于 2013-06-30 21:28:48
一个简单的选择是配置matplotlib以自动调整绘图大小。它对我来说非常好用,我不确定为什么默认情况下没有激活它。
方法1
在matplotlibrc文件中设置它
figure.autolayout : True
有关定制matplotlibrc文件的更多信息,请参见此处:http://matplotlib.org/users/customizing.html
方法2
在运行时更新rcParams,如下所示
from matplotlib import rcParams
rcParams.update({'figure.autolayout': True})
使用这种方法的好处是,您的代码将在不同配置的机器上生成相同的图形。
https://stackoverflow.com/questions/6774086
复制相似问题