1

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This is great thanks! I had to use "set_autoscale_on(True)".
is there a way to autoscale the xticks in a similar way?
@denvar matplotlib should already automatically create xticks for you unless you hard-coded ticks in some way. That's really part of a separate question, but you would need to post some of the code so we could see what was happening.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.