如何在 Matplotlib Python 中設定 X 軸值?


要在 Python 中的 matplotlib 中設定 X 軸值,我們可以採取以下步驟 -

  • 針對 x 和 y 資料點建立兩個 列表

  • 獲取xticks 範圍值。

  • 使用 x 軸範圍值和 y 資料點透過 plot() 方法繪製一條線。

  • 使用 xticks() 方法用 X 軸值替換xticks 

  • 要顯示圖形,請使用 show() 方法。

範例

from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
x = [45, 1, 34, 78, 100]
y = [8, 10, 23, 78, 2]
default_x_ticks = range(len(x))
plt.plot(default_x_ticks, y)
plt.xticks(default_x_ticks, x)
plt.show()

輸出

更新於:2023 年 8 月 22 日

超過 7.6 萬次瀏覽

開啟你的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.