Friday, April 3, 2026
HomeLanguagesChange the x or y interval of a Matplotlib figure

Change the x or y interval of a Matplotlib figure

It is quite easy to change the x or y range using matplotlib.pyplot.xlim() and matplotlib.pyplot.ylim() from the Matplotlib is a library in Python. This function in pyplot module of matplotlib library is used to get or set the x-limits/ y-limits of the current axes and returns the tuple of the new x-axis/y-axis limits.

Syntax: matplotlib.pyplot.xlim(*args, **kwargs)

Parameters: 

*args:

  •  left: This parameter is used to set the xlim/ylim to left.
  • right: This parameter is used to set the xlim/ylim to right.

 **kwargs: This is Text properties that is used to control the appearance of the labels.

Example 1: Without using matplotlib.pyplot.xlim()/matplotlib.pyplot.ylim() function.

Python3




import numpy as np
import matplotlib.pyplot as plt
  
x = [0, 5, 9, 10, 15, 20, 25]
y = [0, 1, 2, 3, 4, 5, 6]
  
plt.plot(x, y)
plt.show()


Output:

Example 2: With the matplotlib.pyplot.xlim()/matplotlib.pyplot.ylim() function.

Python3




import numpy as np
import matplotlib.pyplot as plt
  
x = [0,5,9,10,15,20,25]
y = [0,1,2,3,4,5,6]
plt.xlim([-40, 40])
plt.ylim([-40, 40])
  
plt.plot(x,y)
plt.show()


Output:-

RELATED ARTICLES

Most Popular

Dominic
32512 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6885 POSTS0 COMMENTS
Nicole Veronica
12006 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12100 POSTS0 COMMENTS
Shaida Kate Naidoo
7015 POSTS0 COMMENTS
Ted Musemwa
7259 POSTS0 COMMENTS
Thapelo Manthata
6971 POSTS0 COMMENTS
Umr Jansen
6960 POSTS0 COMMENTS