其他分享
首页 > 其他分享> > matplotlib 多条曲线动态显示

matplotlib 多条曲线动态显示

作者:互联网

只保存缓存的数据

import matplotlib.pyplot as plt
import numpy as np

POINTS = 10
x_list = [0] * POINTS
sin_list = [0] * POINTS
cos_list = [0] * POINTS
indx = 0

while True:
    y = []
    indx += 1
    # 更新绘图数据
    x_list = x_list[1:] + [indx]
    sin_list = sin_list[1:] + [np.sin((indx / 20) * np.pi)]
    cos_list = cos_list[1:] + [np.cos((indx / 20) * np.pi)]
    y.append([sin_list, 'sin_list'])
    y.append([cos_list, 'cos_list'])
    # 显示时间
    plt.pause(0.01)
    # 清除上一次显示
    plt.cla()
    for i in range(len(y)):
        plt.plot(x_list, y[i][0], label=y[i][1])
    plt.legend()

保存原有数据

import matplotlib.pyplot as plt
import numpy as np

POINTS = 10
x_list = [0] * POINTS
sin_list = [0] * POINTS
cos_list = [0] * POINTS
indx = 0

while True:
    y = []
    indx += 1
    # 更新绘图数据
    x_list.append(indx)
    sin_list.append(np.sin((indx / 20) * np.pi))
    cos_list.append(np.cos((indx / 20) * np.pi))
    y.append([sin_list, 'sin_list'])
    y.append([cos_list, 'cos_list'])
    # 显示时间
    plt.pause(0.01)
    # 清除上一次显示
    plt.cla()
    for i in range(len(y)):
        plt.plot(x_list, y[i][0], label=y[i][1])
    plt.legend()

标签:动态显示,cos,plt,list,多条,matplotlib,indx,np,sin
来源: https://blog.csdn.net/bbmmbbmm20/article/details/122340452