Here is my MWE:
import matplotlib.pyplot as plt import numpy as np plt.rcParams.update({'font.size': 12}) fig = plt.figure(figsize=(8, 6), layout='constrained') ax = fig.add_subplot(111, projection='3d') # Data zline = np.random.randint(0, 10, 50) xline = np.random.randint(0, 10, 50) yline = np.random.randint(0, 10, 50) ax.scatter(xline, yline, zline, 'gray', s=10) ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') # ax.set_box_aspect(None, zoom=0.9) ax.view_init(30, -135, 0) plt.show() fig.savefig("test-figure.png", dpi=150, bbox_inches='tight') There is a answer here: Labels are cut off but it is not working for me. The z label is still cut off.
I try another methods: Stop matplotlib 3D surface plot from having axis labels cut off and matplotlib 3D plot Z label cut off by adding ax.set_box_aspect(None, zoom=0.85). But it also adds extra space at the right, top, and bottom of the figure.
May I ask if there are any methods to save the figure using a tight layout without adding any spaces and preserving the z label?
Edit: The installed Python version is 3.11.8, and the Matplotlib version is 3.8.3.


fig.tight_layout()beforefig.savefig?bbox_inches='tight'in the call tofif.savefig()?fig.savefig()with or withoutbbox_inches='tight'have no effect, the z label still gets cut off.layout="tight"(or equivalently, removinglayout="constrained"and addingfig.tight_layout()before saving) and removingbbox_inches="tight"results in a figure with the z-label, although it has a bit of extra space on the sides (because of the figure size settings; changing it to(7,6)improves that).