Home > Software engineering >  How to adjust subplots borders in matplotlib automatically?
How to adjust subplots borders in matplotlib automatically?

Time:01-04

When plotting multiple plots using plt.subplots, most of the time the spacing between subplots is not ideal so the the xtick labels of the top plot would overlap with the title of the bottom plots. There is a way to fix this manually by calling say plt.subplots_adjust(hspace=0.5) and changing the parameters interactively to obtain a decent looking plot. Is there a way to calculate the subplot_adjust parameter automatically? Meaning finding the minimum hspace and wspace so that there is not overlap between texts of the plots.

CodePudding user response:

You can use tight_layout https://matplotlib.org/stable/tutorials/intermediate/tight_layout_guide.html or constrained_layout https://matplotlib.org/stable/tutorials/intermediate/constrainedlayout_guide.html

CodePudding user response:

I'm pretty certain that the closest your going to find to an inbuilt calculation method is:

plt.tight_layout()

or

figure.Figure.tight_layout() #if you are using the object version of the code
  •  Tags:  
  • Related