2014-02-20 66 views
13

如何旋轉z標籤以便文本讀取(bottom => top)而不是(top => bottom)?在3D matplotlib中旋轉軸標籤文本

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設置是什麼。這似乎是一個old feature request on github但它沒有工作。有沒有解決方法?

+1

有興趣知道答案。 –

回答

14

作爲一種變通方法,您可以手動設置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()