简体   繁体   中英

Re-adjusting (automatically) limits on plot in matplotlib

Is there a way to let matplotlib know to recompute the optimal bounds of a plot?

My problem is that, I am manually computing a bunch of boxplots, putting them at various locations in a plot. By the end, some boxplots extend beyond the plot frame. I could hard-code some xlim and ylim's for now, but I want a more general solution.

What I was thinking was a feature where you say "ok plt I am done plotting, now please adjust the bounds so that all my data is nicely within the bounds".

Is this possible?

EDIT: The answer is yes.

Follow-up question: Can this be done for the ticks as well?

You want to use matplotlib's automatic axis scaling. You can do this with either axes.axis with the "auto" input or axes.set_autoscale_on

ax.axis('auto')
ax.set_autoscale_on()

If you want to auto-scale only the x or y axis, you can use set_autoscaley_on or set_autoscalex_on .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM