在 matplotlib 中的動態繪圖過程中移動 X 軸
要在即時繪圖過程中移動 matplotlib 中的 X 軸,我們可採取以下步驟 -
- 設定影像大小,並調整子繪圖之間的和周圍的邊距。
- 建立一個圖形和一組子繪圖。
- 使用 numpy 建立 x 和 y 資料點。
- 使用 plot() 方法繪製 x 和 y 資料點。
- 透過重複呼叫移動即時繪圖中 X 軸的函式 *animate* 來製作動畫。
- 要顯示圖形,請使用 show() 方法。
示例
import matplotlib.pylab as plt import matplotlib.animation as animation import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() x = np.linspace(0, 15, 100) y = np.cos(x) ax.plot(x, y, lw=2, color='red') def animate(frame): ax.set_xlim(left=0, right=frame) ani = animation.FuncAnimation(fig, animate, frames=10) plt.show()
輸出
廣告