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:-
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.