Home > Back-end >  How to move plot to the left of window in matplotlib
How to move plot to the left of window in matplotlib

Time:01-21

I have a plot with a very wide legend. I've managed to move the legend out of the plot so that it doesn't cover it, but the legend is too wide for the window and not completely visible. This could be corrected if I knew how to move plot and legend towards the left where there is spare space. This is what I mean: enter image description here What instruction would allow me to do this? My current code:

f,ax=plt.subplots(1)

f.set_size_inches(14,10.5)

...

plt.legend(bbox_to_anchor=(1,1), loc="upper left")

plt.show() 

Thank you

CodePudding user response:

You can try adjusting the margins. The line to do that is f.subplots_adjust(left=0.05, bottom=0.07, right=0.95, top=0.95, wspace=0, hspace=0). These values can be controlled from the button to the left of the save button on the bottom toolbar. So, you can try playing with those in the gui, then when you find a value you like or that works, enter them in the suggested line of code.

Then you code should look like .

f,ax=plt.subplots(1)

f.set_size_inches(14,10.5)

...

plt.legend(bbox_to_anchor=(1,1), loc="upper left")
f.subplots_adjust(left=0.05, bottom=0.07, right=0.95, top=0.95, wspace=0, hspace=0)

plt.show() 
  •  Tags:  
  • Related