使用 Matplotlib 新增額外的軸刻度


要新增額外刻度,我們可以採取以下步驟 −

  • 使用 numpy 建立 x 和 y 點。

  • 在曲線上繪製 x 和 y 點,x 刻度可能為曲線上的 1 到 10(100 個數據點)。

  • 為新增額外刻度,使用 xticks() 方法,並將刻度範圍從 1 到 10 增加到 1 到 20。

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

示例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(1, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.xticks(range(1, 20))
plt.show()

輸出

更新時間:2021 年 4 月 9 日

13,000+ 瀏覽量

開啟你的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.