python matplotlib从函数更新散点图 [英] python matplotlib update scatter plot from a function

查看:90
本文介绍了python matplotlib从函数更新散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动更新散点图. 我的X和Y值的来源是外部的,并且数据在非预期的时间间隔(轮数)中自动推送到我的代码中.

I am trying to automatically update a scatter plot. The source of my X and Y values is external, and the data is pushed automatically into my code in a non-predicted time intervals (rounds).

我只设法在整个过程结束时绘制所有数据,而我试图不断地将数据添加并绘制到画布中.

I have only managed to plot all the data when the whole process ended, whereas I am trying to constantly add and plot data into my canvas.

我得到的(在整个运行结束时)是这样的:

What I DO get (at the end of the whole run) is this:

而我所追求的是:

我的代码的简化版本:

import matplotlib.pyplot as plt

def read_data():
    #This function gets the values of xAxis and yAxis
    xAxis = [some values]  #these valuers change in each run
    yAxis = [other values] #these valuers change in each run

    plt.scatter(xAxis,yAxis, label  = 'myPlot', color = 'k', s=50)     
    plt.xlabel('x')
    plt.ylabel('y')
    plt.show()

推荐答案

有几种方法可以使matplotlib图动画.在下面,我们来看两个使用散点图的最小示例.

There are several ways to animate a matplotlib plot. In the following let's look at two minimal examples using a scatter plot.

要制作动画,我们需要一个事件循环.获取事件循环的一种方法是使用plt.ion()(交互式打开").然后需要先绘制图形,然后可以循环更新图形.在循环内部,我们需要绘制画布并为窗口引入一些暂停以处理其他事件(例如鼠标交互等).没有此暂停,窗口将冻结.最后,我们调用plt.waitforbuttonpress()以使窗口即使在动画结束后也保持打开状态.

For an animation to take place we need an event loop. One way of getting the event loop is to use plt.ion() ("interactive on"). One then needs to first draw the figure and can then update the plot in a loop. Inside the loop, we need to draw the canvas and introduce a little pause for the window to process other events (like the mouse interactions etc.). Without this pause the window would freeze. Finally we call plt.waitforbuttonpress() to let the window stay open even after the animation has finished.

import matplotlib.pyplot as plt
import numpy as np

plt.ion()
fig, ax = plt.subplots()
x, y = [],[]
sc = ax.scatter(x,y)
plt.xlim(0,10)
plt.ylim(0,10)

plt.draw()
for i in range(1000):
    x.append(np.random.rand(1)*10)
    y.append(np.random.rand(1)*10)
    sc.set_offsets(np.c_[x,y])
    fig.canvas.draw_idle()
    plt.pause(0.1)

plt.waitforbuttonpress()

(b)使用FuncAnimation

可以使用 matplotlib.animation.FuncAnimation 自动执行上述操作. FuncAnimation将负责循环和重绘,并会在给定的时间间隔后不断调用函数(在本例中为animate()).动画将仅在调用plt.show()之后开始,从而自动在绘图窗口的事件循环中运行.

(b) using FuncAnimation

Much of the above can be automated using matplotlib.animation.FuncAnimation. The FuncAnimation will take care of the loop and the redrawing and will constantly call a function (in this case animate()) after a given time interval. The animation will only start once plt.show() is called, thereby automatically running in the plot window's event loop.

import matplotlib.pyplot as plt
import matplotlib.animation
import numpy as np

fig, ax = plt.subplots()
x, y = [],[]
sc = ax.scatter(x,y)
plt.xlim(0,10)
plt.ylim(0,10)

def animate(i):
    x.append(np.random.rand(1)*10)
    y.append(np.random.rand(1)*10)
    sc.set_offsets(np.c_[x,y])

ani = matplotlib.animation.FuncAnimation(fig, animate, 
                frames=2, interval=100, repeat=True) 
plt.show()

这篇关于python matplotlib从函数更新散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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