问题:为什么在matplotlib图中我的xlabel被截断了?

我正在使用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)$')
plt.show()

虽然您可以看到整个ylabel,但xlabel在底部被切除了。

如果这是机器特定的问题,我将在带有matplotlib 1.0.0的OSX 10.6.8上运行此问题

I am plotting a dataset using matplotlib where I have an xlabel that is quite “tall” (it’s a formula rendered in TeX that contains a fraction and is therefore has the height equivalent of a couple of lines of text).

In any case, the bottom of the formula is always cut off when I draw the figures. Changing figure size doesn’t seem to help this, and I haven’t been able to figure out how to shift the x-axis “up” to make room for the xlabel. Something like that would be a reasonable temporary solution, but what would be nice would be to have a way to make matplotlib recognize automatically that the label is cut off and resize accordingly.

Here’s an example of what I mean:

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)$')
plt.show()

while you can see the entire ylabel, the xlabel is cut off at the bottom.

In the case this is a machine-specific problem, I am running this on OSX 10.6.8 with matplotlib 1.0.0


回答 0

采用:

import matplotlib.pyplot as plt

plt.gcf().subplots_adjust(bottom=0.15)

为标签腾出空间。

编辑:

自从我给出答案以来,matplotlib增加了tight_layout()功能。所以我建议使用它:

plt.tight_layout()

应该为xlabel腾出空间。

Use:

import matplotlib.pyplot as plt

plt.gcf().subplots_adjust(bottom=0.15)

to make room for the label.

Edit:

Since i gave the answer, matplotlib has added the tight_layout() function. So i suggest to use it:

plt.tight_layout()

should make room for the xlabel.


回答 1

一个简单的选项是将matplotlib配置为自动调整绘图大小。它非常适合我,我不确定为什么默认情况下未激活它。

方法一

在您的matplotlibrc文件中进行设置

figure.autolayout : True

有关自定义matplotlibrc文件的更多信息,请参见此处:http : //matplotlib.org/users/customizing.html

方法2

像这样在运行时更新rcParams

from matplotlib import rcParams
rcParams.update({'figure.autolayout': True})

使用这种方法的优点是您的代码将在配置不同的计算机上生成相同的图形。

An easy option is to configure matplotlib to automatically adjust the plot size. It works perfectly for me and I’m not sure why it’s not activated by default.

Method 1

Set this in your matplotlibrc file

figure.autolayout : True

See here for more information on customizing the matplotlibrc file: http://matplotlib.org/users/customizing.html

Method 2

Update the rcParams during runtime like this

from matplotlib import rcParams
rcParams.update({'figure.autolayout': True})

The advantage of using this approach is that your code will produce the same graphs on differently-configured machines.


回答 2

如果要将其存储到文件中,可以使用bbox_inches="tight"参数来解决:

plt.savefig('myfile.png', bbox_inches = "tight")

In case you want to store it to a file, you solve it using bbox_inches="tight" argument:

plt.savefig('myfile.png', bbox_inches = "tight")

回答 3

plot.tight_layout()所有更改放在图表上,show()savefig()将解决问题。

Putting plot.tight_layout() after all changes on the graph, just before show() or savefig() will solve the problem.


回答 4

plt.autoscale() 为我工作。

plt.autoscale() worked for me.


回答 5

您还可以$HOME/.matplotlib/matplotlib_rc按照以下方式将自定义填充设置为默认值。在下面的示例中,我同时修改了底部和左侧的填充:

# The figure subplot parameters.  All dimensions are a fraction of the
# figure width or height
figure.subplot.left  : 0.1 #left side of the subplots of the figure
#figure.subplot.right : 0.9 
figure.subplot.bottom : 0.15
...

You can also set custom padding as defaults in your $HOME/.matplotlib/matplotlib_rc as follows. In the example below I have modified both the bottom and left out-of-the-box padding:

# The figure subplot parameters.  All dimensions are a fraction of the
# figure width or height
figure.subplot.left  : 0.1 #left side of the subplots of the figure
#figure.subplot.right : 0.9 
figure.subplot.bottom : 0.15
...

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。