只保存缓存的数据
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()