-1

I have a problem with shifting the label of z axis in z direction (along the axis) in 3d plot. I want to have the label of Z-axis at its end. I found that it should be possible for example by ax.zaxis.set_label_coords(0,0,10) but it does not work.

I am using the following code to generate the plot:

from mpl_toolkits.mplot3d import Axes3D

%matplotlib notebook

fig = plt.figure(figsize=(10,7))
ax = fig.gca(projection='3d')

ax.xaxis.set_rotate_label(False)
ax.set_xlabel(r'$X$',fontsize=16,rotation=0)
ax.set_xlim3d(0, 9)
ax.yaxis.set_rotate_label(False)
ax.set_ylabel(r'$Y$',fontsize=16,rotation=0)
ax.set_ylim3d(0.35, 0.58)
ax.zaxis.set_rotate_label(False)
ax.set_zlabel(r'$Z$',fontsize=16,rotation=0, labelpad=5)
ax.zaxis.set_label_coords(0,0,10)
ax.set_zlim3d(0, 10)

ax.view_init(elev=30., azim=220)

plt.show()
Zephyr
  • 11,891
  • 53
  • 45
  • 80
dgizer
  • 1
  • 1

1 Answers1

0

Though I have no idea why set_label_coords() doesn't work in 3d plot, you can use ax.text() instead to draw the z label.

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


fig = plt.figure(figsize=(10,7))
ax = fig.gca(projection='3d')

ax.xaxis.set_rotate_label(False)
ax.set_xlabel(r'$X$',fontsize=16,rotation=0)
ax.set_xlim3d(0, 9)

ax.yaxis.set_rotate_label(False)
ax.set_ylabel(r'$Y$',fontsize=16,rotation=0)
ax.set_ylim3d(0.35, 0.58)

ax.zaxis.set_rotate_label(False)
ax.set_zlabel(r'$Z$',fontsize=16,rotation=0, labelpad=5)
ax.set_zlim3d(0, 10)

ax.text(x=-1, y=0.58, z=11, s="z", color='red', size=8)

ax.view_init(elev=30., azim=220)

plt.show()

There are already some questions about set_label_coords() in 3d plot, but none gives a reason why it doesn't work.

enter image description here

Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52