【问题标题】:Rotating axes label text in 3D matplotlib3D matplotlib中的旋转轴标签文本
【发布时间】:2014-02-20 20:07:10
【问题描述】:

如何旋转 z-label 以使文本显示为 (bottom => top) 而不是 (top => bottom)?

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()

无论我的ax.azim 设置是什么,我都希望它保持不变。这似乎是一个old feature request on github,但没有关于它的工作。有解决办法吗?

【问题讨论】:

  • 也有兴趣知道答案。

标签: python matplotlib mplot3d


【解决方案1】:

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

ax.zaxis.set_rotate_label(False)  # disable automatic rotation
ax.set_zlabel('label text', rotation=90)

请注意,z-label 的方向也取决于您的观点,例如:

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()

更新: 也可以调整已经绘制(但不是事先)的绘图的 z-label 方向。这是修改标签的调整版本:

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-28
    • 2015-03-19
    • 1970-01-01
    • 2012-06-15
    相关资源
    最近更新 更多