如何獨立設定繪圖的水平和垂直主要刻度和次要刻度網格線?


為了設定繪圖的水平和垂直主要刻度和次要刻度網格線,我們可以使用 grid() 方法。

步驟

  • 設定圖形尺寸並調整子圖之間的間距和周圍的間距。
  • 建立一個圖形和一組子圖。
  • 為主要刻度製作水平網格線。
  • 在軸上定位次要定位器。
  • 使用 grid() 方法制作次要網格線。
  • 要顯示圖形,請使用 show() 方法。

例子

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator

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

fig, ax = plt.subplots()

ax.yaxis.grid(which="major", color='r', linestyle='-', linewidth=2)
ml = MultipleLocator(0.10)

ax.xaxis.set_minor_locator(ml)
ax.xaxis.grid(which="minor", color='k', linestyle='-.', linewidth=0.7)

plt.show()

輸出

更新於: 15-Jun-2021

897 次瀏覽

啟動你的 職業

完成課程並獲得認證

開始
廣告
© . All rights reserved.