更改 Matplotlib 图形的 x 或 y 间隔
使用 matplotlib.pyplot.xlim() 和 matplotlib.pyplot.ylim() 更改 x 或 y 范围非常容易,Matplotlib is a library in Python。 matplotlib 库的 pyplot 模块中的该函数用于获取或设置当前轴的 x-limits/y-limits,并返回新的 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.
示例 1:不使用 matplotlib.pyplot.xlim()/matplotlib.pyplot.ylim()函数。
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()
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()
输出:
示例 2:使用 matplotlib.pyplot.xlim()/matplotlib.pyplot.ylim()函数。
蟒蛇3
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()
输出:-