How to Set Axis Ranges in Matplotlib?
Last Updated :
12 Jun, 2025
Matplotlib sets the default range of the axis by finding extreme values (i.e. minimum and maximum) on that axis. However, to get a better view of data sometimes the Pyplot module is used to set axis ranges of the graphs according to the requirements in Matplotlib. Let’s create a basic sine wave plot to use throughout the examples:
Python
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.1) # 0 to 10
y = np.sin(x) # sine of X
plt.plot(x, y, color='blue')
plt.title("Default Axis Range")
plt.show()
Output
Basic Sine WaveUsing plt.xlim() and plt.ylim()
If you want to quickly set the limits of the x-axis and y-axis, you can use plt.xlim() and plt.ylim(). These functions let you specify exactly what part of the graph you want to see.
Python
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.1)
y = np.sin(x)
plt.plot(x, y, color='blue')
plt.xlim(2, 6) # Set x-axis
plt.ylim(-1, 1) # Set y-axis
plt.show()
Output
Using plt.xlim() and plt.ylim()Explanation: plt.xlim(2, 6) zooms in on the x-values from 2 to 6, while plt.ylim(-1, 1) limits the y-axis to the sine wave’s natural range, keeping the plot focused and tidy.
Using ax.set_xlim() and ax.set_ylim()
When you’re working with more complex plots like multiple subplots and it’s better to use Matplotlib’s object-oriented style. This means you work with Axes objects directly and set the axis limits on them.
Python
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.1)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y, color='blue')
ax.set_xlim(2, 6)
ax.set_ylim(-1, 1)
plt.show()
Output
Using ax.set_xlim() and ax.set_ylim()Explanation: ax.set_xlim(2, 6) zooms the x-axis to values 2–6 and ax.set_ylim(-1, 1) limits the y-axis to the sine wave’s range, providing clear, focused control via the Axes object.
Auto- scaling after ploting
If you’ve manually changed your plot or added new data and want Matplotlib to recalculate the best axis limits automatically, use plt.autoscale(). This refreshes the axes so the view fits the data tightly.
Python
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.1)
y = np.sin(x)
plt.plot(x, y, color='blue')
plt.autoscale(enable=True, axis='both', tight=True)
plt.show()
Output
Using plt.autoscale()Explanation: plt.autoscale(enable=True, axis='both', tight=True) automatically adjusts both x and y axes to tightly fit the data range, ensuring the sine wave fills the plot neatly without extra margins.
Using plt.axis()
Sometimes you want a concise way to set all the axis boundaries at once. plt.axis() lets you do that by passing a list of [xmin, xmax, ymin, ymax].
Python
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.1)
y = np.sin(x)
plt.plot(x, y, color='blue')
plt.axis([2, 5, -1, 1]) # [xmin, xmax, ymin, ymax]
plt.show()
Output
Using plt.axis()Explanation: plt.axis([2, 5, -1, 1]) sets both x and y axis limits at once, zooming the x-axis from 2 to 5 and restricting the y-axis to the sine wave’s natural range of -1 to 1 for a focused, clear view.