Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotating axes label text in 3D matplotlib

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

enter image description here

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?

like image 295
Hooked Avatar asked Feb 20 '14 20:02

Hooked


People also ask

How do I rotate axis labels in MatPlotLib?

Rotate X-Axis Tick Labels in Matplotlib There are two ways to go about it - change it on the Figure-level using plt. xticks() or change it on an Axes-level by using tick. set_rotation() individually, or even by using ax.


1 Answers

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) 

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

gives enter image description here

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() 
like image 167
Dietrich Avatar answered Oct 04 '22 02:10

Dietrich