如何在 Matplotlib 中設定次要刻度線的位置?
若要設定 Matplotlib 中次要刻度線的位置,我們可以採取以下步驟 −
- 設定圖形大小並調整子圖之間的間距以及周圍的間距。
- 使用 numpy 建立 x 和 y 資料點。
- 建立一個圖形和一組子圖。
- 使用 plot() 方法繪出 x 和 y 資料點。
- 若要查詢次要刻度線,請使用 set_minor_locator() 方法。
- 若要顯示次要刻度線,請使用 grid(which='minor')。
- 若要顯示圖形,請使用 show() 方法。
示例
import matplotlib.pyplot as plt import numpy as np from matplotlib.ticker import AutoMinorLocator plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(1, 10, 100) y = np.log(x) fig, ax = plt.subplots() ax.plot(x, y) minor_locator = AutoMinorLocator(2) ax.xaxis.set_minor_locator(minor_locator) plt.grid(which='minor') plt.show()
輸出
廣告