When I plot data using Matplotlib and limit the x-axis to a smaller range, Matplotlib still uses the same y-axis limits as before the x-axis limitation. For example
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-10,10,100)
y = x**2
fig, ax = plt.subplots()
ax.plot(x,y)
ax.set_xlim(-2, 2)
plt.show()
gives the following plot:
I would expect that the y-axis approximately ranges from ~0 to ~4, since these are the new data limits in the x-limited region. Is this considered to be a bug? And if not, is there a simple way to achieve the desired output (without truncation of the NumPy array to the desired x-range)?