调整 Axes3D 标签位置
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我分享你的沮丧。我花了足足半个小时的时间,却一无所获。文档说 set_xlabel 采用 arg labelpad 但我得到错误(AttributeError:未知属性标签)!在 xaxis 或 w_xaxis 上事后设置它不会执行任何操作。
这是一个粗略的解决方法:
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:
为每个轴添加这个,调整数字:
亚当休斯在链接中提到它不起作用,但它对我有用。
Add this for each axis, adapt the number:
It is mentioned in the link by Adam Hughes as not working, but it works for me.
在新版本的 matplotlib 中,操作方法如下:
请参阅此处的说明:
https://github .com/matplotlib/matplotlib/issues/3610
在 v1.4 上测试,应该适用于版本 > 1.1 我相信。
In new versions of matplotlib, this is how to do it:
See the explanation here:
https://github.com/matplotlib/matplotlib/issues/3610
Tested on v1.4, should work in versions > 1.1 I believe.
我真的需要更频繁地关注 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.
这会一次性更改所有 (x, y, z) 标签的填充。我最喜欢这种方法:
This changes the padding for all (x, y, z) labels in one shot. I like this approach the most:
您可以在绘制图形后使用
labelpad
:这可能会导致轴标签超出 3D 绘图,因此您必须使用
dist
方法调整距离:You can use
labelpad
after that you plot your figure:This might cause the axis labels to go outside the 3D plot so you have to adjust the distance using the
dist
method:作为一种设计实践,转换后的文本不太清晰。
我建议您为轴使用标签,也许是颜色编码。
中执行此操作的方法
这是在 matplotlib
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
我正在使用 mpl 3.4.3,但仍然遇到这个问题。我应该注意我正在使用 3D 子图,也许这是我的问题?
我觉得我已经尝试了一切,包括这里的答案。我只是无法将刻度标签或轴标签放在我想要的位置。
在不触及填充的情况下(请注意,左图的 zaxis 标签不在图中):
,以下内容将标签推得很远:
所以我认为减少这些数字应该会让它们更接近。以下内容使 yaxis(实现)非常接近刻度标签,但不是 xaxis 和 zaxis 标签:
在 xaxis 和 zaxis 上稍微调整一点没有任何作用:
抱歉,这不是答案,但我觉得我在这里提出的调整和意外行为可能有助于激发适当的答案?
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?
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):
The following pushes the labels quite far away, as expected:
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:
Tweaking a bit harder on xaxis and zaxis doesn't do anything:
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?