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

 时间:2026-02-13 17:15:47

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绘制随机散点的动态图

  • 小米南瓜粥的做法
  • Win版OneNote如何不显示表格边框?
  • 社交中的"黄金原则"有哪些?
  • 怎么进行磁盘碎片整理?整理时要注意什么?
  • 怎样设置页面对话框
  • 热门搜索
    情人节快乐英文怎么写 炖鸡翅的家常做法 桦怎么读 point怎么读 鸡蛋炒面的家常做法 腌萝卜怎么腌好吃又脆 对不起用英语怎么说 icloud储存空间已满怎么解决 披萨饼的做法视频 姜汤的做法 感冒