如何去掉 matplotlib 座標軸的刻度和標記?


若要關閉 matplotlib 座標軸 的刻度和標記,我們可以採取以下步驟 -

  • 設定圖形大小並調整子圖之間和周圍的空白。
  • 使用 numpy 建立 x 和 y 資料點。
  • 使用 plot() 方法繪製 x 和 y 資料點。
  • 獲取當前作圖的座標軸。
  • 使用 set_tick_params() 隱藏 X 和 Y 座標軸標籤標記。
  • 使用 set_xticks()set_yticks() 隱藏 X 和 Y 座標軸刻度標記。
  • 若要顯示圖形,使用 show() 方法。

示例

import numpy as np
from matplotlib import 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(-10, 10, 100)
y = np.sin(x)

plt.plot(x, y)
ax = plt.gca()

# Hide X and Y axes label marks
ax.xaxis.set_tick_params(labelbottom=False)
ax.yaxis.set_tick_params(labelleft=False)

# Hide X and Y axes tick marks
ax.set_xticks([])
ax.set_yticks([])

plt.show()

輸出

它將產生以下輸出

更新於: 2023-9-10

47K+ 瀏覽量

開始你的職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.