使用matplotlib进行动画处理,其中将点动态添加到图形中 [英] Animation with matplotlib where points are dynamically added to a graph

查看:125
本文介绍了使用matplotlib进行动画处理,其中将点动态添加到图形中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个简单的代码,该代码使用while循环在某些值之间生成随机点(x0, y0).设置完每个点的坐标后,该点将绘制到一个空白图形中,该图形显示在while循环的末尾.

I've written a simple code which generates random points (x0, y0) between certain values using a while loop. After the coordinates of each point are set, that point is drawn in an empty graph which is showed at the end of the while loop.

但是,我想使用matplotlib设置动画,这样我就可以看到初始图形,并在代码计算它们时逐渐将点添加到其中.我已经寻找了一些示例,但是我发现的示例主要与wave有关,等等,我想我需要一种略有不同的方法.

However, I would like to set up an animation with matplotlib which would allow me to see the initial graph and the points progressively added to it as the code is calculating them. I've looked for some examples but the ones I found are mainly concerned with waves and so on and I guess I need a slightly different approach.

这是基本代码:

from numpy import *
from pylab import *
import random

figure(figsize=(8,6), dpi=150)
x = np.linspace(-1, 4.5, 250)

h=5
a=0.5
b=4

ylim(-0.5,5.5)
xlim(-1,5.0)

i= 0

while i< 500:
    R1 = random.random()
    R2 = random.random()
    x0 = (b - a)*R1 + a
    y0 = h*R2
    scatter(x0, y0, 10, color="red")
    i = i + 1

show()  

感谢您的帮助!

动画代码

import numpy as np
import matplotlib.pyplot as plt
from pylab import *
import matplotlib.animation as animation
import random

fig = plt.figure(figsize=(8,6), dpi=150)
x = np.linspace(-2, 4.5, 250)


h=4
a=1
b=3

hlines(y=h, xmin=1, xmax=3, linewidth=1.5)
vlines(x=a, ymin=0, ymax=4, linewidth=1.5)
vlines(x=b, ymin=0, ymax=4, linewidth=1.5)

ylim(-2.5,10.5)
xlim(-2.5,4.5)
grid()

def data_gen():
    i = 0
    while i< 1:
        R1 = random.random()
        R2 = random.random()
        x0 = (b - a)*R1 + a
        y0 = h*R2
        i = i + 1
        yield x0, y0


line, = plot([], [], linestyle='none', marker='o', color='r')

ax = gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))

xdata, ydata = [], []

def run(data):
    x0,y0 = data
    xdata.append(x0)
    ydata.append(y0)
    line.set_data(xdata, ydata)

    return line,

ani = animation.FuncAnimation(fig, run, data_gen, blit=True, interval=0.5,
    repeat=False)
plt.show()

推荐答案

我不知道这是否正是您要的内容;无论如何,您都可以在run函数内部生成随机点,并在其中进行绘制.您既不需要blit = True也不需要清除从一帧到另一帧的轴.
这是我的代码:

I do not know if this is exactly what you are looking for; in any case, you can generate random points inside the run function and there plot them. You do not need neither blit = True nor clear the axis from one frame to another.
Here is my code:

from pylab import *
from matplotlib.animation import FuncAnimation
import random

fig = plt.figure(figsize=(8,6), dpi=150)
x = np.linspace(-2, 4.5, 250)

h=4
a=1
b=3

hlines(y=h, xmin=a, xmax=b, linewidth=1.5)
vlines(x=a, ymin=0, ymax=h, linewidth=1.5)
vlines(x=b, ymin=0, ymax=h, linewidth=1.5)

ylim(-2.5,10.5)
xlim(-2.5,4.5)
grid()

ax = gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))

def run(i):
        R1 = random.random()
        R2 = random.random()
        x0 = (b - a)*R1 + a
        y0 = h*R2
        ax.scatter(x0, y0, 10, color='red')

ani = FuncAnimation(fig = fig, func = run, frames = 500, interval = 10, repeat = False)
plt.show()

产生以下动画:

(为了获得更轻的文件(小于2 MB,我将该动画切成100点;上面的代码产生了500点的动画)

(I cut this animation to 100 points in order to get a lighter file, less than 2 MB; the code above produces an animation wiht 500 points)

这篇关于使用matplotlib进行动画处理,其中将点动态添加到图形中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆