Saturday, November 16, 2024
Google search engine
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

Recent Comments