调整 Axes3D 标签位置

发布于 2024-10-29 17:44:44 字数 570 浏览 4 评论 0原文

我在 matplotlib 中的轴标签与刻度标签重叠时遇到问题。我尝试通过应用转换或调用 set_y() 来“手动”重新定位标签,但无济于事。

下面是重现该问题的代码片段:

import matplotlib
matplotlib.use("TKAGG")
import matplotlib.pyplot as pyplot
import mpl_toolkits.mplot3d

figure = pyplot.figure()
figure.subplots_adjust(bottom=0.25, top=0.75)
axes = figure.gca(projection='3d')
xLabel = axes.set_xlabel('XXX xxxxxx xxxx x xx x')
yLabel = axes.set_ylabel('YY (y) yyyyyy')
zLabel = axes.set_zlabel('Z zzzz zzz (z)')
plot = axes.plot([1,2,3],[1,2,3])

pyplot.show()

请注意 x 和 y 标签如何与刻度线发生冲突。我可以优雅地解决这个问题吗?

I am having trouble with axes labels overlapping ticks labels in matplotlib. I've tried to reposition the labels "manually" by applying transforms or by calling set_y(), but no avail.

Here's a snippet that reproduces the problem:

import matplotlib
matplotlib.use("TKAGG")
import matplotlib.pyplot as pyplot
import mpl_toolkits.mplot3d

figure = pyplot.figure()
figure.subplots_adjust(bottom=0.25, top=0.75)
axes = figure.gca(projection='3d')
xLabel = axes.set_xlabel('XXX xxxxxx xxxx x xx x')
yLabel = axes.set_ylabel('YY (y) yyyyyy')
zLabel = axes.set_zlabel('Z zzzz zzz (z)')
plot = axes.plot([1,2,3],[1,2,3])

pyplot.show()

Note how the x and y labels clash with the ticks. Can I solve this elegantly ?

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

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

发布评论

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

评论(8

青衫负雪 2024-11-05 17:44:44

我分享你的沮丧。我花了足足半个小时的时间,却一无所获。文档说 set_xlabel 采用 arg labelpad 但我得到错误(AttributeError:未知属性标签)!在 xaxis 或 w_xaxis 上事后设置它不会执行任何操作。

这是一个粗略的解决方法:

import matplotlib
matplotlib.use("TKAGG")
import matplotlib.pyplot as pyplot
import mpl_toolkits.mplot3d

figure = pyplot.figure(figsize=(8,4), facecolor='w')
ax = figure.gca(projection='3d')

xLabel = ax.set_xlabel('\nXXX xxxxxx xxxx x xx x', linespacing=3.2)
yLabel = ax.set_ylabel('\nYY (y) yyyyyy', linespacing=3.1)
zLabel = ax.set_zlabel('\nZ zzzz zzz (z)', linespacing=3.4)
plot = ax.plot([1,2,3],[1,2,3])
ax.dist = 10

pyplot.show()

在此处输入图像描述

I share your frustration. I worked on it for a good half hour and got nowhere. The docs say set_xlabel takes an arg labelpad but I get an error (AttributeError: Unknown property labelpad)! Setting it after the fact doesn't do anything, on xaxis or w_xaxis.

Here's a crude workaround:

import matplotlib
matplotlib.use("TKAGG")
import matplotlib.pyplot as pyplot
import mpl_toolkits.mplot3d

figure = pyplot.figure(figsize=(8,4), facecolor='w')
ax = figure.gca(projection='3d')

xLabel = ax.set_xlabel('\nXXX xxxxxx xxxx x xx x', linespacing=3.2)
yLabel = ax.set_ylabel('\nYY (y) yyyyyy', linespacing=3.1)
zLabel = ax.set_zlabel('\nZ zzzz zzz (z)', linespacing=3.4)
plot = ax.plot([1,2,3],[1,2,3])
ax.dist = 10

pyplot.show()

enter image description here

执手闯天涯 2024-11-05 17:44:44

为每个轴添加这个,调整数字:

axes.yaxis.labelpad=30

亚当休斯在链接中提到它不起作用,但它对我有用。

Add this for each axis, adapt the number:

axes.yaxis.labelpad=30

It is mentioned in the link by Adam Hughes as not working, but it works for me.

煮茶煮酒煮时光 2024-11-05 17:44:44

在新版本的 matplotlib 中,操作方法如下:

ax.xaxis._axinfo['label']['space_factor'] = 2.8

请参阅此处的说明:

https://github .com/matplotlib/matplotlib/issues/3610

在 v1.4 上测试,应该适用于版本 > 1.1 我相信。

In new versions of matplotlib, this is how to do it:

ax.xaxis._axinfo['label']['space_factor'] = 2.8

See the explanation here:

https://github.com/matplotlib/matplotlib/issues/3610

Tested on v1.4, should work in versions > 1.1 I believe.

忘羡 2024-11-05 17:44:44

我真的需要更频繁地关注 StackOverflow。我是 mplot3d 的当前维护者。通常在常规 2d 绘图中起作用的各种技巧不适用于 3d 绘图的原因是因为 mplot3d 最初是用硬编码默认值编写的。 mplot3d 计算渲染标签的角度的方式也存在错误。

v1.1.0 包含多项修复以改善现状。我修复了轴标签角度的错误计算,并对间距进行了一些调整。对于下一个版本,我希望 3d 轴占用的空间大于默认轴间距,因为默认设计是考虑到刻度标签和轴标签将位于轴之外,而 mplot3d 则不是这种情况。由于间距是由 mplot3d 中的相对比例决定的,因此较小的工作空间会迫使标签靠得更近。

至于其他可能的解决方法,请参阅注释 此处
公平的警告是,这个私有字典并不是一个永久的解决方案,而是在 mplot3d 重构完成之前必要的邪恶。

此外,v1.1.0 包含对 mplot3d api 的许多更新。请查看修订后的文档此处< /a>.

I really need to follow StackOverflow more often. I am the current maintainer of mplot3d. The reason why the various tricks that typically work in regular 2d plots don't work for 3d plots is because mplot3d was originally written up with hard-coded defaults. There were also bugs in how mplot3d calculated the angle to render the labels.

v1.1.0 contains several fixes to improve the state of things. I fixed the miscalculation of axes label angles, and I made some adjustments to the spacing. For the next release, I would like to have 3d axes to take up more than the default axes spacing, since the default was designed to take into account that tick labels and axes labels would be outside the axes, which is not the case for mplot3d. Because the spacings are determined by relative proportions in mplot3d, having a smaller space to work within forces the labels closer together.

As for other possible avenues for work-arounds, please see the note here.
A fair warning, this private dictionary is not intended to be a permanent solution, but rather a necessary evil until the refactor of mplot3d is complete.

Also, v1.1.0 contains many updates to the api of mplot3d. Please check out the revised documentation here.

小…红帽 2024-11-05 17:44:44

这会一次性更改所有 (x, y, z) 标签的填充。我最喜欢这种方法:

from matplotlib import rcParams
rcParams['axes.labelpad'] = 20

This changes the padding for all (x, y, z) labels in one shot. I like this approach the most:

from matplotlib import rcParams
rcParams['axes.labelpad'] = 20
雪花飘飘的天空 2024-11-05 17:44:44

您可以在绘制图形后使用 labelpad

axes = fig.gca(projection='3d')
axes.xaxis.labelpad=20
axes.yaxis.labelpad=20
axes.zaxis.labelpad=20

这可能会导致轴标签超出 3D 绘图,因此您必须使用 dist 方法调整距离:

ax.dist = 13

You can use labelpad after that you plot your figure:

axes = fig.gca(projection='3d')
axes.xaxis.labelpad=20
axes.yaxis.labelpad=20
axes.zaxis.labelpad=20

This might cause the axis labels to go outside the 3D plot so you have to adjust the distance using the dist method:

ax.dist = 13
再可℃爱ぅ一点好了 2024-11-05 17:44:44

作为一种设计实践,转换后的文本不太清晰。
我建议您为轴使用标签,也许是颜色编码。
中执行此操作的方法

import matplotlib
matplotlib.use("TKAGG")
import matplotlib.pyplot as pyplot
import mpl_toolkits.mplot3d

figure = pyplot.figure()
figure.subplots_adjust(bottom=0.25, top=0.75)
axes = figure.gca(projection='3d')
xLabel = axes.set_xlabel('X', fontsize=14, fontweight='bold', color='b')
yLabel = axes.set_ylabel('Y',fontsize=14, fontweight='bold', color='r')
zLabel = axes.set_zlabel('Z',fontsize=14, fontweight='bold', color='g')


x = pyplot.Rectangle((0, 0), 0.1, 0.1,fc='b')
y = pyplot.Rectangle((0, 0), 0.1, 0.1,fc='r')
z = pyplot.Rectangle((0, 0), 0.1, 0.1,fc='g')

handles, labels = axes.get_legend_handles_labels()
axes.legend((x,y,z),("XXXXXX","YYYYY","ZZZZZZ"),'best')


plot = axes.plot([1,2,3],[1,2,3])


pyplot.show()

这是在 matplotlib Image result

As a design practice, transformed text is not very legible.
I would suggest you to use labels for your axis, maybe color encoded.
This is how you do it in matplotlib

import matplotlib
matplotlib.use("TKAGG")
import matplotlib.pyplot as pyplot
import mpl_toolkits.mplot3d

figure = pyplot.figure()
figure.subplots_adjust(bottom=0.25, top=0.75)
axes = figure.gca(projection='3d')
xLabel = axes.set_xlabel('X', fontsize=14, fontweight='bold', color='b')
yLabel = axes.set_ylabel('Y',fontsize=14, fontweight='bold', color='r')
zLabel = axes.set_zlabel('Z',fontsize=14, fontweight='bold', color='g')


x = pyplot.Rectangle((0, 0), 0.1, 0.1,fc='b')
y = pyplot.Rectangle((0, 0), 0.1, 0.1,fc='r')
z = pyplot.Rectangle((0, 0), 0.1, 0.1,fc='g')

handles, labels = axes.get_legend_handles_labels()
axes.legend((x,y,z),("XXXXXX","YYYYY","ZZZZZZ"),'best')


plot = axes.plot([1,2,3],[1,2,3])


pyplot.show()

Image result

鹿港巷口少年归 2024-11-05 17:44:44

我正在使用 mpl 3.4.3,但仍然遇到这个问题。我应该注意我正在使用 3D 子图,也许这是我的问题?

fig = plt.figure(figsize=(3,2))
ax = fig.add_subplot(1,2,i,projection='3d')

我觉得我已经尝试了一切,包括这里的答案。我只是无法将刻度标签或轴标签放在我想要的位置。

在不触及填充的情况下(请注意,左图的 zaxis 标签不在图中):

,以下内容将标签推得很远:

ax.xaxis.labelpad = 20
ax.yaxis.labelpad = 20
ax.zaxis.labelpad = 20

在此处输入图像描述

所以我认为减少这些数字应该会让它们更接近。以下内容使 yaxis(实现)非常接近刻度标签,但不是 xaxis 和 zaxis 标签:

ax.xaxis.labelpad = 1
ax.yaxis.labelpad = 1
ax.zaxis.labelpad = 1

在此处输入图像描述

在 xaxis 和 zaxis 上稍微调整一点没有任何作用:

ax.xaxis.labelpad = 0.1
ax.yaxis.labelpad = 1
ax.zaxis.labelpad = 0.001

在此处输入图像描述

抱歉,这不是答案,但我觉得我在这里提出的调整和意外行为可能有助于激发适当的答案?

I'm using mpl 3.4.3 and I am still having this issue. I should note I am using 3D subplots, perhaps that is my issue?

fig = plt.figure(figsize=(3,2))
ax = fig.add_subplot(1,2,i,projection='3d')

I feel like I have tried everything, including answers here. I just cannot get my tick labels or axis label where I want it.

Without touching the padding I have (note the zaxis label for the left plot is off the fig):
enter image description here

The following pushes the labels quite far away, as expected:

ax.xaxis.labelpad = 20
ax.yaxis.labelpad = 20
ax.zaxis.labelpad = 20

enter image description here

So I figure reducing those numbers should bring them closer. The following brings the yaxis (realizations) quite close to the tick labels, but not xaxis and zaxis labels:

ax.xaxis.labelpad = 1
ax.yaxis.labelpad = 1
ax.zaxis.labelpad = 1

enter image description here

Tweaking a bit harder on xaxis and zaxis doesn't do anything:

ax.xaxis.labelpad = 0.1
ax.yaxis.labelpad = 1
ax.zaxis.labelpad = 0.001

enter image description here

Sorry this isn't an answer, but I feel like the adjustments I present here and the unexpected behavior may help stimulate an appropriate answer?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文