实时动态绘图

3

我有一个关于使用matplotlib动态更新散点图的问题。

我在Python中有以下类:

''' PolygonHandler.py - Python source for polygon handling '''
import numpy as np
import matplotlib.pyplot as plt


class PolygonHandler:
    # Constructor
    def __init__(self):
        self.X = []
        self.Y = []
        self.numberPoints = 0

    # Print the polygon
    def draw(self):
        plt.scatter(self.X,self.Y)
        plt.draw()

    def update(self):
        for i in range(1,self.numberPoints):
            self.X[i] += np.random.normal()*0.01
            self.Y[i] += np.random.normal()*0.01


    # append a point
    def add(self,x,y):
        self.numberPoints += 1
        self.X.append(x)
        self.Y.append(y)

这个类用于在实时循环中接收信息并将点添加到PolygonHandler类中。现在,为了举例说明,我想设计以下循环:

P = PolygonHandler()
P.add(1,1)
P.add(2,2)
P.add(1,2)
plt.ion()
plt.show()
while (True):
    P.draw()
    P.update()

我应该如何告诉解释器绘制散点图,并在更新后删除旧的点?目前,我的图绘制了所有点及其先前位置。

Vincent

非常感谢您的帮助。

PS:我遇到的另一个问题是,由matplotlib打开的窗口一旦被点击(例如将其移动到屏幕上的另一个位置)就会冻结并停止响应。有没有办法防止这种情况发生?


为什么在不需要旧数据点的情况下需要“更新”画图?难道你不能关闭旧图并只绘制新数据吗? - Schorsch
1
谢谢您的回答!实际上我正在将这段代码用于一个实时运行的C++应用程序中:不幸的是,关闭和重新打开太慢了。 - Vincent
2个回答

1

这里有一种方法可以使用matplotlib中的动画。

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

class PolygonHandler:
    # Constructor
    def __init__(self):
        self.X = []
        self.Y = []
        self.numberPoints = 0
        self.fig , ax = plt.subplots()
        self.sc = ax.scatter(self.X,self.Y)
        ax.set_xlim(0,3)
        ax.set_ylim(0,3)

    # Print the polygon
    def update(self,_):
        for i in range(self.numberPoints):
            self.X[i] += np.random.normal()*0.01
            self.Y[i] += np.random.normal()*0.01
        self.sc.set_offsets(np.column_stack((self.X,self.Y)))
        return self.sc,

    # append a point
    def add(self,x,y):
        self.numberPoints += 1
        self.X.append(x)
        self.Y.append(y)

这样你就可以绘制出三条随机游走路径:

P = PolygonHandler()
P.add(1,1)
P.add(2,2)
P.add(1,2)
ani = animation.FuncAnimation(P.fig, P.update, interval=10,blit=False)

关键元素是方法set_offsets(),它替换了散点图的数据。然后通过update()返回此类散点对象,以便matplotlib知道必须更新它。有一个由类处理的matplotlib动画的另一个来源,请参见this matplotlib示例。
在最后一行中使用blit=True可以加快动画速度,但根据您的操作系统可能无法正常工作。

0

你可以这样做。它接受 x,y 作为列表,并在同一图上输出散点图和线性趋势。

from IPython.display import clear_output
from matplotlib import pyplot as plt
%matplotlib inline
    
def live_plot(x, y, figsize=(7,5), title=''):
    clear_output(wait=True)
    plt.figure(figsize=figsize)
    plt.xlim(0, training_steps)
    plt.ylim(0, 100)
    x= [float(i) for i in x]
    y= [float(i) for i in y]
    
    if len(x) > 1:
        plt.scatter(x,y, label='axis y', color='k') 
        m, b = np.polyfit(x, y, 1)
        plt.plot(x, [x * m for x in x] + b)

    plt.title(title)
    plt.grid(True)
    plt.xlabel('axis x')
    plt.ylabel('axis y')
    plt.show();

你只需要在循环中调用live_plot(x, y)。这是它的样子: 输入图像描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接