3D Matplotlib中的旋转轴标签文本 [英] Rotating axes label text in 3D matplotlib

查看:436
本文介绍了3D Matplotlib中的旋转轴标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何旋转z标签,使文本显示为(底部=>顶部)而不是(顶部=>底部)?

How do I rotate the z-label so the text reads (bottom => top) rather than (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设置如何,我都希望保留它.这似乎是github上的旧功能请求,但尚无任何工作.有解决方法吗?

I want this to hold no matter what my ax.azim setting is. This seems to be an old feature request on github but there isn't a work on it. Is there a workaround?

推荐答案

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

As a workaround, you could set the direction of the z-label manually by:

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

请注意,z标签的方向也取决于您的视点,例如:

Please note that the direction of your z-label also depends on your viewpoint, e.g:

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标签方向.这是修改标签的调整后版本:

Update: It is also possible, to adjust the z-label direction of a plot, which is already drawn (but not beforehand). This is the adjusted version to modify the labels:

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

这篇关于3D Matplotlib中的旋转轴标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆