Home > OS >  How not to have a cut matplotlib plot
How not to have a cut matplotlib plot

Time:11-15

General Issue: My visualisation is cut on the edges and I found a way to correct it manually. But is there a command that could do it itself ?

I'm using Pyzo to code Python (Python 3.9.5) with Matplotlib.pyplot (version 3.4.2)

  • When I want to see a graph that I plotted (with plot.show()) command I get the plot in a separate window but it appeared cut (especially the edges : titles) as shown in the image below : Cut plot

  • Then, I tried to put it in full-window. It is slightly better but still cut (especially the top) : Still cut

So I started looking into the parameters which were accessible in the Subplot Configuration Tool, and by changing the topvalue, I was able to make my plot completely viewable:

  • Subplot Configuration Tool:Subplot Configuration Tool
  • After modification of top value to 0.95:Good visualisation

Thus, I was wondering if there was a way to change the value directly in the code instead of having to deal with it manually each time.

Note : In the past, I had to use figsize=(x_width,y_width) as a parameter in plt.subplots() but it doesn't change anything here (at most, I can have the first picture above)

Recap :

  • Is there a way to make sure, nothing will be cut in the visualisation of the plot (Title of the plot Titles of the axis) with a command ?

CodePudding user response:

  1. you can do this manually with plt.subplots_adjust() (https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.subplots_adjust.html)
  2. you can do this automatically with plt.figure(constrained_layout=True) (https://matplotlib.org/stable/tutorials/intermediate/constrainedlayout_guide.html)
  3. you can do this automatically with plt.tight_layout() instead of using constrained_layout. However, in general constrained layout is more flexible. https://matplotlib.org/stable/tutorials/intermediate/tight_layout_guide.html
  • Related