如何使用 matplotlib 在 X 軸上追加一個帶有標籤的刻度?


要使用 matplotlib 在 X 軸上追加一個帶有標籤的刻度,我們可以採取以下步驟。

步驟

  • 設定圖片大小,調整子圖之間的內邊距和周圍的內邊距。

  • 使用 numpy 建立xy 資料點。

  • 使用plot() 方法繪製 x 和 y 資料點。

  • 在一個點設定xticks

  • 設定單個刻度點的刻度標籤

  • 要顯示圖片,請使用Show() 方法。

舉例

import numpy as np
import matplotlib.pyplot as plt

# Set the figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create x and y data points
x = np.linspace(-5, 5, 50)
y = np.sin(x)

# Plot x and y data points
fig, ax = plt.subplots(1, 1)
p = ax.plot(x, y)

# Set xticks at a point
ax.set_xticks([-1.075])

# Set xticklabels for the point
ax.set_xticklabels(["$\bf{It\ is -\!1.075\ label}$"])

# Display the plot
plt.show()

輸出

它將產生以下輸出 −

更新於:2021 年 10 月 9 日

2K+ 瀏覽

開啟你的 職業生涯

完成課程以取得認證

入門
廣告
© . All rights reserved.