如何在Matplotlib中更改X轴和Y轴的范围?
要更改 X 和 Y 轴的范围,我们可以使用 xlim() 和 ylim() 方法。
步骤
- 设置图形大小并调整子图之间和周围的间距。
- 使用 numpy 创建 x 和 y 数据点。
- 使用 plot() 方法绘制 x 和 y 数据点。
- 设置X轴和Y轴的限制。
- 使用 show() 方法显示图形。
示例
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(-15, 15, 100)
y = np.sin(x)
plt.plot(x, y)
plt.xlim(-10, 10)
plt.ylim(-1, 1)
plt.show()