在 matplotlib 中繪製不同長度的兩個不同陣列
要在 matplotlib 中繪製不同長度的兩個不同陣列,我們可以採取以下步驟 −
- 設定圖形大小並調整子圖之間的和周圍的邊距。
- 使用 numpy 建立具有不同陣列長度的 y1、x1、y2 和 x2 資料點。
- 使用 plot() 方法繪製 x1、y1 和 x2、y2 資料點。
- 要顯示圖形,請使用 show() 方法。
示例
import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True y1 = (np.random.random(100) - 0.5).cumsum() y2 = y1.reshape(-1, 10).mean(axis=1) x1 = np.linspace(0, 1, 100) x2 = np.linspace(0, 1, 10) plt.plot(x1, y1) plt.plot(x2, y2) plt.show()
輸出
它將產生以下輸出
廣告