如何使用截止或重叠标签调整填充 [英] How to adjust padding with cutoff or overlapping labels

查看:20
本文介绍了如何使用截止或重叠标签调整填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用子图更新了 MRE

  • 我不确定原始问题和 MRE 的用处.对于较大的 x 和 y 标签,边距填充似乎已适当调整.
  • 该问题可通过子图重现.
  • 使用matplotlib 3.4.2

fig, axes = plt.subplots(ncols=2, nrows=2, figsize=(8, 6))轴 = 轴.flatten()对于轴中的 ax:ax.set_ylabel(r'$lnleft(frac{x_a-x_b}{x_a-x_c}
ight)$')ax.set_xlabel(r'$lnleft(frac{x_a-x_d}{x_a-x_e}
ight)$')plt.show()

原创

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

无论如何,当我绘制图形时,公式的底部总是被切断.更改图形大小似乎对此没有帮助,而且我无法弄清楚如何将 x 轴向上"移动.为 xlabel 腾出空间.类似的东西将是一个合理的临时解决方案,但最好有一种方法让 matplotlib 自动识别标签被切断并相应地调整大小.

这是我的意思的一个例子:

将 matplotlib.pyplot 导入为 pltplt.figure()plt.ylabel(r'$lnleft(frac{x_a-x_b}{x_a-x_c}
ight)$')plt.xlabel(r'$lnleft(frac{x_a-x_d}{x_a-x_e}
ight)$', fontsize=50)plt.title('matplotlib 3.4.2 示例
MRE 不再是问题')plt.show()

整个 ylabel 是可见的,但是,xlabel 在底部被切断了.

如果这是一个特定于机器的问题,我将在 OSX 10.6.8 和 matplotlib 1.0.0 上运行它

解决方案

使用:

将 matplotlib.pyplot 导入为 pltplt.gcf().subplots_adjust(bottom=0.15)# 没有 .gcf 的替代选项plt.subplots_adjust(底部=0.15)

为标签腾出空间,其中

Updated MRE with subplots

  • I'm not sure of the usefulness of the original question and MRE. The margin padding seems to be properly adjusted for large x and y labels.
  • The issue is reproducible with subplots.
  • Using 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'$lnleft(frac{x_a-x_b}{x_a-x_c}
ight)$')
    ax.set_xlabel(r'$lnleft(frac{x_a-x_d}{x_a-x_e}
ight)$')

plt.show()

Original

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'$lnleft(frac{x_a-x_b}{x_a-x_c}
ight)$')
plt.xlabel(r'$lnleft(frac{x_a-x_d}{x_a-x_e}
ight)$', fontsize=50)
plt.title('Example with matplotlib 3.4.2
MRE no longer an issue')
plt.show()

The entire ylabel is visible, however, 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

解决方案

Use:

import matplotlib.pyplot as plt

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

# alternate option without .gcf
plt.subplots_adjust(bottom=0.15)

to make room for the label, where plt.gcf() means get the current figure. plt.gca(), which gets the current Axes, can also be used.

Edit:

Since I gave the answer, matplotlib has added the plt.tight_layout() function.

See matplotlib Tutorials: Tight Layout Guide

So I suggest using it:

fig, axes = plt.subplots(ncols=2, nrows=2, figsize=(8, 6))
axes = axes.flatten()

for ax in axes:
    ax.set_ylabel(r'$lnleft(frac{x_a-x_b}{x_a-x_c}
ight)$')
    ax.set_xlabel(r'$lnleft(frac{x_a-x_d}{x_a-x_e}
ight)$')

plt.tight_layout()
plt.show()

这篇关于如何使用截止或重叠标签调整填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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