如何在 Python Matplotlib 中指定 Y 轴上的值?


要在 Python 中指定 Y 轴上的值,我们可以按以下步骤操作:-

  • 使用 numpy 创建 x 和 y 数据点。
  • 要指定轴的值,请创建一个字符列表。
  • 分别使用 xticksyticks 方法指定 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()

输出

更新于:2021 年 5 月 6 日

12K+ 浏览量

开启你的 职业生涯

通过完成课程获得认证

开始
广告