1

I'm drawing a simple 3D scatter plot using matplotlib; however, the Z axes label moves outside the bounding box. I tried adjusting the size, using tight.layout, and many more option, but I wasn't able to move the plot in such a way that the Z axes label is visible. Here is my code:

fig = plt.figure(figsize = (7,7), dpi = 200)
ax = fig.add_subplot(projection='3d')

xs = df_final_depth['Power']
ys = df_final_depth['Velocity']
z = df_final_depth['Predicted Depth (um)']
zs = df_final_depth['Ground Truth Depth (um)']

ax.scatter(xs, ys, z, s=70, label="Predicted Depth ($\mu$$m$)", marker='o')
ax.scatter(xs, ys, zs, label="Ground Truth Depth ($\mu$$m$)", marker='^')

ax.set_xlabel("Power ($W$)")
ax.set_ylabel("Scanning Velocity ($mm/s$)",)
ax.set_zlabel("Depth ($\mu$$m$)")
#ax.tick_params()
ax.legend()
plt.show()

enter image description here

Please refer to the attached pictures. Tried tight.layout, changing the figure size and many other options. Can someone advise on solving this issue?

1 Answers1

0

One option is to add a padding to the Z ticks and labels.

So we need to use ZAxis.labelpad and Axes.tick_params and adjust the following parameters :

labelpad The distance between the axis label and the tick labels.

pad
Distance in points between tick and label.

Update/Add these two lines in/to your code and make sure the adjust the values (if needed) :

ax.set_zlabel("Depth ($\mu$$m$)", labelpad=-10) # Updated

ax.tick_params(axis="z", pad=-3) # (+) Added

Output :

enter image description here

Timeless
  • 22,580
  • 4
  • 12
  • 30