用python绘制随机散点的动态图

2025-10-23 15:39:15

1、加载画图模块和数据处理模块:

# -*- coding: utf-8 -*-   

  

import numpy as np  

import matplotlib.pyplot as plt  

用python绘制随机散点的动态图

2、画一个绿色的随机折线图:

fig = plt.figure()  

img = fig.add_subplot(111)  

img.plot(np.random.rand(36),'green')  

plt.show()  

用python绘制随机散点的动态图

3、想要构造动态图,还需要加载一个动态模块:

import matplotlib.animation as antt

用python绘制随机散点的动态图

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()

用python绘制随机散点的动态图

5、加快速度:

gif = antt.FuncAnimation(fig, dtt, dong, interval=200)  

plt.show()

interval=200指的是,帧间时间间隔是200毫秒。

用python绘制随机散点的动态图

6、不要把倒数第二行前面的等号去掉,否则没有动态图:

antt.FuncAnimation(fig, dtt, dong, interval=200)  

plt.show()

尽管在后面,这个定义未被调用,也不能丢掉。

用python绘制随机散点的动态图

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()

用python绘制随机散点的动态图

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()

用python绘制随机散点的动态图

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢