如何在 Python Matplotlib 中指定 Y 轴的值?
为了在 Python 中指定 Y 轴的值,可以执行以下步骤-
- 使用 numpy 创建 x 和 y 数据点。
- 创建一个包含字符的列表来指定轴的值。
- 使用 xticks 和 yticks 方法分别指定 x 和 y 轴的刻度,并使用 x 和 y 轴的刻度数据点。
- 使用 plot() 方法绘制线条,使用 x 和 y、 color=red 参数进行控制。
- 将 x 和 y 轴的边距设置为 0。
- 使用 show() 方法显示图表。
示例
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.array([0, 2, 4, 6])
y = np.array([1, 3, 5, 7])
ticks = ['a', 'b', 'c', 'd']
plt.xticks(x, ticks)
plt.yticks(y, ticks)
plt.plot(x, y, c='green')
plt.margins(x=0, y=0)
plt.show()