3

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.

enter image description here

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.

enter image description here

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.

6
  • 1
    Have you tried adding fig.tight_layout() before fig.savefig? Commented Feb 25, 2024 at 16:41
  • Did you try leaving out bbox_inches='tight' in the call to fif.savefig()? Commented Feb 25, 2024 at 19:36
  • @jared I've already tried that, it's still not working. You can test with my MWE above. Commented Feb 25, 2024 at 20:00
  • @JohanC I just tested, both fig.savefig() with or without bbox_inches='tight' have no effect, the z label still gets cut off. Commented Feb 25, 2024 at 20:01
  • Changing to layout="tight" (or equivalently, removing layout="constrained" and adding fig.tight_layout() before saving) and removing bbox_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). Commented Feb 25, 2024 at 22:38

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.