如何在簡單 UI 中為 Python 程式顯示即時圖表?
為 Python 程式在簡單 UI 中顯示即時圖表,我們可以啟用等值線圖。
步驟
設定圖形大小並調整子圖之間的邊距。
建立 10×10 維度的隨機資料。
利用 **subplots()** 方法建立一個圖形和一組子圖。
透過 **FuncAnimation()** 類反覆呼叫函式 **func** 來生成動畫。
為了在函式中更新等值線的值,我們可以定義一個 **animate()** 方法,它可用於 **FuncAnimation()** 類中。
利用 **show()** 方法來顯示圖形。
示例
import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.randn(800).reshape(10, 10, 8) fig, ax = plt.subplots() def animate(i): ax.clear() ax.contourf(data[:, :, i], cmap="copper") ani = animation.FuncAnimation(fig, animate, 5, interval=100, blit=False) plt.show()
輸出
廣告