在3D中旋转轴标签文本

33
如何将z轴标签旋转,使文本从底部到顶部而不是从顶部到底部阅读?
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_zlabel('label text flipped', rotation=90) 
ax.azim = 225
plt.show()

enter image description here

我希望无论我的 ax.azim 设置如何,都能实现这一点。这似乎是一个 在 GitHub 上的旧功能请求,但目前还没有相关工作。是否有其他解决方法?

1
也很想知道答案。 - m_power
1个回答

35

作为一种解决方法,您可以通过以下方式手动设置z轴标签的方向:

ax.zaxis.set_rotate_label(False)  # disable automatic rotation
ax.set_zlabel('label text', rotation=90)
请注意,z轴标签的方向也取决于您的视点,例如:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fg = plt.figure(1); fg.clf()
axx = [fg.add_subplot(4,1,1+i, projection='3d') for i in range(4)]
for ax,azel in zip(axx, [(115,10), (115,-10), (-115,10), (-115,-10)]):
    ax.set_title(u"Azim, elev = {}°, {}°".format(*azel))
    ax.set_zlabel('label text')
    ax.azim, ax.elev = azel

fg.canvas.draw()
plt.show()

提供enter image description here

更新:也可以调整已绘制的图形的z标签方向,而不是预先设置。这是修改标签的调整版本:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fg = plt.figure(1); fg.clf()
axx = [fg.add_subplot(4,1,1+i, projection='3d') for i in range(4)]
for ax,azel in zip(axx, [(115,10), (115,-10), (-115,10), (-115,-10)]):
    ax.set_title(u"Azim, elev = {}°, {}°".format(*azel))
    ax.set_zlabel('label text')
    ax.azim, ax.elev = azel
fg.canvas.draw()  # the angles of the text are calculated here

# Read drawn z-label rotations and switch them if needed
for ax in axx:
   ax.zaxis.set_rotate_label(False)
   a = ax.zaxis.label.get_rotation()
   if a<180:
       a += 180
   ax.zaxis.label.set_rotation(a)
   a = ax.zaxis.label.get_rotation() # put the actual angle in the z-label
   ax.set_zlabel(u'z-rot = {:.1f}°'.format(a))
fg.canvas.draw()

plt.show()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接