为什么我的matplotlib图中的xlabel被截断了?

编程入门 行业动态 更新时间:2024-10-24 09:19:19
本文介绍了为什么我的matplotlib图中的xlabel被截断了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用matplotlib绘制数据集,其中我有一个非常高"的xlabel(这是在TeX中渲染的公式,其中包含一个分数,因此其高度等于几行文本).

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).

无论如何,在绘制数字时,公式的底部总是被截断.更改图形大小似乎无济于事,而且我还无法弄清楚如何将x轴向上"移动以为xlabel腾出空间.这样的事情是一个合理的临时解决方案,但是最好的办法是使matplotlib自动识别标签被切开并相应地调整大小.

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.

这是我的意思的示例:

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在底部被切除了.

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

在这是机器特定的问题的情况下,我正在使用matplotlib 1.0.0在OSX 10.6.8上运行它

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

推荐答案

使用:

import matplotlib.pyplot as plt plt.gcf().subplots_adjust(bottom=0.15)

为标签腾出空间.

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

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

plt.tight_layout()

应该为xlabel腾出空间.

should make room for the xlabel.

更多推荐

为什么我的matplotlib图中的xlabel被截断了?

本文发布于:2023-10-17 13:10:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1501015.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图中   matplotlib   xlabel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!