用python绘制随机散点的动态图
1、加载画图模块和数据处理模块:
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt

2、画一个绿色的随机折线图:
fig = plt.figure()
img = fig.add_subplot(111)
img.plot(np.random.rand(36),'green')
plt.show()

3、想要构造动态图,还需要加载一个动态模块:
import matplotlib.animation as antt

4、绘制随机动态图:
fig = plt.figure()
img = fig.add_subplot(111)
a, = img.plot(np.random.rand(36),'green')
def dtt(data):
a.set_ydata(data)
return a,
def dong():
while True:
yield np.random.rand(36)
gif = antt.FuncAnimation(fig, dtt, dong, interval=1000)
plt.show()

5、加快速度:
gif = antt.FuncAnimation(fig, dtt, dong, interval=200)
plt.show()
interval=200指的是,帧间时间间隔是200毫秒。

6、不要把倒数第二行前面的等号去掉,否则没有动态图:
antt.FuncAnimation(fig, dtt, dong, interval=200)
plt.show()
尽管在后面,这个定义未被调用,也不能丢掉。

7、前后随机数的数目必须一致。
fig = plt.figure()
img = fig.add_subplot(111)
a, = img.plot(np.random.rand(365),'green')
def dtt(data):
a.set_ydata(data)
return a,
def dong():
while True:
yield np.random.rand(365)
b = antt.FuncAnimation(fig, dtt, dong, interval=200)
plt.show()

8、增加折线数量,改变颜色:
fig = plt.figure()
img = fig.add_subplot(111)
a, = img.plot(np.random.rand(2018),'red')
def dtt(data):
a.set_ydata(data)
return a,
def dong():
while True:
yield np.random.rand(2018)
b = antt.FuncAnimation(fig, dtt, dong, interval=200)
plt.show()
