I'd like to plot some points using matplotlib (pyplot.plot
, scatter
, or errorbar
), apply the limits using xlim
and then call a function that will adjust the Y axis limits to match the points that are visible in automated way.
MWE:
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(-10, 10)
plt.plot(x, x, 'ro')
plt.xlim(-5, 5)
plt.show()
And of course the Y axis limits are approximately (-11, 10)
, but I'd like them to be approx. (-5, 5)
. I've tried following matplotlib functions:
relim
, set_ylim(auto=True)
, autoscale
, autoscale_view
and none of them worked. I know I could remember all the points that were plotted, limit them to given range of X values, find min and max, and set limits based on that, but I'm looking for simpler way to do that.