Search code examples
pythonmatplotlibscatter3d

Z-label does not show up in 3d matplotlib scatter plot


The z-label does not show up in my figure. What is wrong?

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
plt.show()

Output

Neither ax.set_zlabel("z") nor ax.set(zlabel="z") works. The x- and y-labels work fine.


Solution

  • That's a padding issue.

    labelpadfloat The distance between the axis label and the tick labels. Defaults to rcParams["axes.labelpad"] (default: 4.0) = 4.

    You can use matplotlib.axis.ZAxis.labelpad to adjust this value for the z-axis :

    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection="3d")
    ax.set_xlabel("x")
    ax.set_ylabel("y")
    ax.set_zlabel("StackOverflow", rotation=90)
    ax.zaxis.labelpad=-0.7 # <- change the value here
    plt.show();
    

    Output :

    enter image description here