Thursday, September 4, 2025
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
32263 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11857 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6696 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS