如何在 Matplotlib 中移除軸刻度中帶小數點的數字?


若要移除 Matplotlib 中軸刻度中的小數點後數字,我們可執行以下步驟 −

  • 設定影像大小並調整子圖間和周圍的內邊距。

  • 使用 numpy 建立 xy 資料點。

  • 建立一張影像和一組子圖。

  • 若要僅將 xtick 標籤設定為數字,我們可使用 x.astype(int) 方法。

  • 若要顯示影像,請使用 show() 方法。

示例

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

x = np.array([1.110, 2.110, 4.110, 5.901, 6.00, 7.90, 8.90])
y = np.array([2.110, 1.110, 3.110, 9.00, 4.001, 2.095, 5.890])

fig, ax = plt.subplots()

ax.plot(x, y)

ax.set_xticklabels(x.astype(int))

plt.show()

輸出

更新日期: 10-Aug-2021

7K+ 瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

入門
廣告
© . All rights reserved.