如何在 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+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.