Jesper - jtk.eth
Jesper - jtk.eth

Reputation: 7436

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?

Upvotes: 1

Views: 2115

Answers (1)

Suever
Suever

Reputation: 65430

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.

Upvotes: 3

Related Questions