How to automatically set ylim from data shown on the screen after setting xlim
matplotlib, plot, python
Solution
This approach will work in case `y(x)` is non-linear. Given the arrays `x` and `y` that you want to plot:
lims = gca().get_xlim()
i = np.where( (x > lims[0]) & (x < lims[1]) )[0]
gca().set_ylim( y[i].min(), y[i].max() )
show()
Problem
For example, after I set xlim, the ylim is wider than the range of data points shown on the screen. Of course, I can manually pick a range and set it, but I would prefer if it is done automatically. Or, at least, how can we determine y-range of data points shown on screen? plot right after I set xlim: plot after I manually set ylim: