向下移动 X 轴标签,但不向下移动 matplotlib 中的 X 轴刻度

发布于 2024-11-16 08:07:10 字数 744 浏览 5 评论 0原文

我正在使用 Matplotlib 绘制直方图。 使用我上一个问题中的提示:Matplotlib - 标记每个垃圾箱, 我或多或少已经解决了这些问题。

最后还有一个问题 - 之前 - x 轴标签(“时间(以毫秒为单位)”)被渲染在 x 轴刻度线(0.00、0.04、0.08、0.12 等)下方

无填充 - 数字下方的轴标签

根据 Joe Kingston 的建议(参见上面的问题),我尝试使用:

ax.tick_params(axis='x', pad=30)

但是,这会移动 x 轴刻度线(0.00、0.04、0.08、0.12 等)以及 x 轴标签(“时间(以毫秒为单位)”):

30 填充 - 轴标签和刻度线均已移动

有没有办法仅将 x 轴标签移动到三个轴的下方一排数字?

注意:您可能需要直接打开下面的 PNG - 右键单击​​图像,然后查看图像(在 FF 中),或在新选项卡中打开图像 (Chrome)。 SO 完成的图像大小调整使它们几乎无法阅读

I'm using Matplotlib to plot a histogram.
Using tips from my previous question: Matplotlib - label each bin,
I've more or less got the kinks worked out.

There's one final issue - previously - the x-axis label ("Time (in milliseconds)") was being rendered underneath the x-axis tickmarks (0.00, 0.04, 0.08, 0.12 etc.)

No padding - Axis label underneath figures

Using the advice from Joe Kingston (see question above), I tried using:

ax.tick_params(axis='x', pad=30)

However, this moves both the x-axis tickmarks (0.00, 0.04, 0.08, 0.12 etc.), as well as the x-axis label ("Time (in milliseconds)"):

30 Padding - Both Axis Label and Tick Marks have Moved

Is there any way to move only the x-axis label to underneath the three rows of figures?

NB: You may need to open the PNGs below directly - Right Click on the image, then View Image (in FF), or Open image in new tab (Chrome). The image resize done by SO has rendered them nigh unreadable

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花桑 2024-11-23 08:07:10

使用labelpad参数:

pl.xlabel("...", labelpad=20)

或在之后设置:

ax.xaxis.labelpad = 20

use labelpad parameter:

pl.xlabel("...", labelpad=20)

or set it after:

ax.xaxis.labelpad = 20
你是年少的欢喜 2024-11-23 08:07:10

如果变量 ax.xaxis._autolabelpos = True,matplotlib 根据(一些摘录)在 axis.py 的函数 _update_label_position 中设置标签位置:

    bboxes, bboxes2 = self._get_tick_bboxes(ticks_to_draw, renderer)
    bbox = mtransforms.Bbox.union(bboxes)
    bottom = bbox.y0
    x, y = self.label.get_position()
    self.label.set_position((x, bottom - self.labelpad * self.figure.dpi / 72.0))

您可以使用以下方法独立于刻度设置标签位置:

    ax.xaxis.set_label_coords(x0, y0)

将 _autolabelpos 设置为 False 或作为上面提到的通过更改labelpad参数。

If the variable ax.xaxis._autolabelpos = True, matplotlib sets the label position in function _update_label_position in axis.py according to (some excerpts):

    bboxes, bboxes2 = self._get_tick_bboxes(ticks_to_draw, renderer)
    bbox = mtransforms.Bbox.union(bboxes)
    bottom = bbox.y0
    x, y = self.label.get_position()
    self.label.set_position((x, bottom - self.labelpad * self.figure.dpi / 72.0))

You can set the label position independently of the ticks by using:

    ax.xaxis.set_label_coords(x0, y0)

that sets _autolabelpos to False or as mentioned above by changing the labelpad parameter.

云醉月微眠 2024-11-23 08:07:10

不要手动填充,而是尝试指定垂直对齐方式 (va=):

ax.set_xlabel("x label", va='top');

如果您像 @HYRY 的答案那样手动修改标签填充,有时当您保存图形时,标签会被剪掉。要包含标签,请在调用 savefig() 时设置 bbox_inches='tight'。例如:

plt.plot(range(10))
plt.xlabel("x label", labelpad=30);
plt.savefig('img.png', bbox_inches='tight');

对于 OOP API,同样是:

fig, ax = plt.subplots()
ax.plot(range(10))
ax.set_xlabel("x label", labelpad=30);
fig.savefig('img.png', bbox_inches='tight');

Instead of padding manually, try assigning vertical alignment (va=):

ax.set_xlabel("x label", va='top');

If you modify the label padding manually as in @HYRY's answer, sometimes when you save the figure the label is cropped off. To include the label, set bbox_inches='tight' when you call savefig(). For example:

plt.plot(range(10))
plt.xlabel("x label", labelpad=30);
plt.savefig('img.png', bbox_inches='tight');

With the OOP API, the same would be:

fig, ax = plt.subplots()
ax.plot(range(10))
ax.set_xlabel("x label", labelpad=30);
fig.savefig('img.png', bbox_inches='tight');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文