如何從Python中的Matplotlib圖形中刪除邊框而不刪除軸刻度標籤?


若要從Matplotlib圖形中刪除邊框而不刪除軸刻度標籤,我們可以採取以下步驟−

  • 設定圖形大小並調整子圖之間的內邊距。
  • 建立y資料點列表。
  • 使用plot()方法繪製y資料點
  • 若要移除左右上下邊的框線,可以使用set_visible()方法。
  • 若要顯示圖形,請使用show()方法。

示例

import matplotlib.pyplot as plt

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

y = [0, 2, 1, 5, 1, 2, 0]

plt.plot(y, color='red', lw=7)

for pos in ['right', 'top', 'bottom', 'left']:
   plt.gca().spines[pos].set_visible(False)

plt.show()

輸出

更新於:07-Jul-2021

2K+瀏覽量

開啟你的職業

完成課程即可獲得認證

開始學習
廣告
© . All rights reserved.