向下移动 X 轴标签,但不向下移动 matplotlib 中的 X 轴刻度
我正在使用 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 轴标签(“时间(以毫秒为单位)”):
有没有办法仅将 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.)
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)"):
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用labelpad参数:
或在之后设置:
use labelpad parameter:
or set it after:
如果变量 ax.xaxis._autolabelpos = True,matplotlib 根据(一些摘录)在 axis.py 的函数 _update_label_position 中设置标签位置:
您可以使用以下方法独立于刻度设置标签位置:
将 _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):
You can set the label position independently of the ticks by using:
that sets _autolabelpos to False or as mentioned above by changing the labelpad parameter.
不要手动填充,而是尝试指定垂直对齐方式 (
va=
):如果您像 @HYRY 的答案那样手动修改标签填充,有时当您保存图形时,标签会被剪掉。要包含标签,请在调用
savefig()
时设置bbox_inches='tight'
。例如:对于 OOP API,同样是:
Instead of padding manually, try assigning vertical alignment (
va=
):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 callsavefig()
. For example:With the OOP API, the same would be: