天天看點

python matplotlib 實時繪圖

import matplotlib.pyplot as plt

ax = []                    # 定義一個 x 軸的空清單用來接收動态的資料
ay = []                    # 定義一個 y 軸的空清單用來接收動态的資料
plt.ion()                  # 開啟一個畫圖的視窗
for i in range(100):       # 周遊0-99的值
    ax.append(i)           # 添加 i 到 x 軸的資料中
    ay.append(i**2)        # 添加 i 的平方到 y 軸的資料中
    plt.clf()              # 清除之前畫的圖
    plt.plot(ax,ay)        # 畫出目前 ax 清單和 ay 清單中的值的圖形
    plt.pause(0.1)         # 暫停一秒
    plt.ioff()             # 關閉畫圖的視窗