如何在 Matplotlib 中繪製指向外部的 R 型軸刻度線?


要使用 matplotlib 繪製指向外部的 R 型(預設是常規型別)軸刻度線,我們可對 X 軸使用 rcParams["xticks.direction"]="out"

步驟

  • 設定圖形大小並調整子圖之間的內邊距和周邊邊距。

  • 使用 plt.rcParams 設定外向 刻度 點。

  • 為資料點數量初始化變數。

  • 使用 numpy 建立 xy 資料點。

  • 使用 plot() 方法繪出 xy 資料點。

  • 使用 show() 方法顯示圖形。

示例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
plt.rcParams['ytick.direction'] = 'out' # in
plt.rcParams['xtick.direction'] = 'out' # in
n = 10
x = np.linspace(-2, 2, n)
y = np.exp(x)
plt.plot(x, y)
plt.show()

輸出


更新日期:01-6 月-2021

183 次瀏覽

開啟你的 事業

透過完成課程獲得認證

開始
Advertisements
© . All rights reserved.