0

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:

Plot image

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)?

Padix Key
  • 857
  • 6
  • 15
  • 1
    Does this answer your question? [How to automatically set the y-axis limits after limiting the x-axis](https://stackoverflow.com/questions/67599526/how-to-automatically-set-the-y-axis-limits-after-limiting-the-x-axis) – jared Jun 18 '23 at 14:35
  • Not completely, since all solutions require either a rather complex functions or truncating the data. Therefore, the answer is probably: No, there is no simple way. – Padix Key Jul 01 '23 at 16:17
  • Using a boolean mask array is fairly simple. – jared Jul 01 '23 at 16:37

0 Answers0