import matplotlib.pyplot as plt
plt.ion()
fig = plt.figure()
ax1 = fig.add_subplot(111)
t = []
v = []
line, = ax1.plot(t, v, linestyle="-", color="r")
import numpy as np
ys = np.random.normal(100, 10, 1000)
for i in range(2000):
t.append(i)
v.append(ys[i])
ax1.set_xlim(min(t), max(t) + 1)
ax1.set_ylim(min(v), max(v) + 1)
line.set_data(t, v)
plt.pause(0.001)
ax1.figure.canvas.draw()
animation版
# _*_ coding:utf-8 _*_
import time
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
x1 = np.arange(0, 20, 1)
y1 = np.random.normal(100, 10, 20)
print(x1.shape)
print(y1.shape)
# format the time style 2016-12-01 00:00:00
x = []
y = []
# initial the size of the figure
fig = plt.figure(figsize=(18, 8), facecolor="white")
# fig.subplots_adjust(left=0.06, right=0.70)
ax1 = fig.add_subplot(111)
# initial plot
p1, = ax1.plot(x, y, linestyle="dashed", color="red")
ax1.set_ylabel("value")
def stream(i):
x.append(x1[i])
y.append(y1[i])
time.sleep(3)#可以执行其他程序,比如等待,删除这个操作也可以
# update the axis
ax1.set_xlim(min(x1), max(x1)+1)
ax1.set_ylim(min(y1), max(y1)+1)
p1.set_data(x, y)
ax1.figure.canvas.draw()
return p1
# main animated function
# for i in range(25):#可以将下面实现放在循环里面
anim = FuncAnimation(fig, stream, frames=len(x1))
plt.show()
参考:可视化篇:流式数据监控(python)



