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

3
  • 1
    Does this answer your question? How to automatically set the y-axis limits after limiting the x-axis Commented Jun 18, 2023 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. Commented Jul 1, 2023 at 16:17
  • Using a boolean mask array is fairly simple. Commented Jul 1, 2023 at 16:37

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.