Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting minimum upper y-lim and maximum lower y-lim in matplotlib

I'm working with the axes objects for a pyplot chart.

What I am trying to do is have the the minimum size for the y-axis, such that the lower end of the axis cannot go above -1 and the upper end of the axis cannot go below 1, but I don't want to constrain an upper bound of the upper end of the axis, and a lower bound for the lower bound of the axis.

Is there a way where I can set the minimum upper bound of the axis and the reverse for the lower one?

like image 870
Andrew Wong Avatar asked Aug 31 '25 00:08

Andrew Wong


1 Answers

So once you perform a plot, just query the axes for the current limits, and extend them if needed. Something like this:

ylim = axes.get_ylim()
new_ylim = (min(ylim[0],-1), max(ylim[1], 1) )
axes.set_ylim(new_ylim)
like image 156
gariepy Avatar answered Sep 02 '25 20:09

gariepy