在 Matplotlib 中使用軸物件來設定活動的子圖
要在 Matplotlib 中設定活動的子圖軸物件,我們可以使用 subplots() 方法將這些軸作為子圖排列新增。
步驟
設定圖形大小並調整子圖之間及其周圍的邊距。
為資料點建立 x 和 y 列表。
使用帶有一行兩列的 subplots() 方法建立一個圖形和一組子圖。
將一個軸新增到當前圖形並將其設為當前軸,軸物件的索引為 0。
使用 plot() 方法繪製 x 和 y 資料點。
將一個軸新增到當前圖形並將其設為當前軸,軸物件的索引為 1。
使用 plot() 方法繪製 x 和 y 資料點。
要顯示圖形,請使用 show() 方法。
示例
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 4, 2, 1] y = [1, 4, 0, 4, 1] fig, axs = plt.subplots(1, 2) plt.axes(axs[0]) plt.plot(x, y) plt.axes(axs[1]) plt.plot(y, x) plt.show()
輸出
廣告