Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 413
Moritz Avatar asked Apr 25 '26 03:04

Moritz


2 Answers

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

like image 183
Timeless Avatar answered Apr 27 '26 16:04

Timeless


Sharing a workaround that resolved the issue for me. To make the z-label visible, I added a title to the figure and padded it with empty spaces to artificially adjust the figure's layout and increase the effective width. Here's the code:

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

# Add a padded title to adjust the layout
ax.set_title(" " * 35 + "Your Title" + " " * 35)
                                      
plt.show()

3D plot with visible z-label

like image 30
Yonatan Goldstein Avatar answered Apr 27 '26 16:04

Yonatan Goldstein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!