如何使用VisPy库实时绘制?

问题描述 投票:1回答:1

我写了一个脚本来模拟大流行的演变(带有图形和散点图)。我尝试了几个库来实时显示结果(8个国家x 500个粒子):

  • Matplotlib(不够快)
  • PyQtGraph(更好,但仍然不够快)
  • OpenGL(很好,但是我没有找到如何有效地在2D中使用子图,标题,图例...)]
  • 散景(很好,但是散点图每次粒子变色时都会“闪烁”。如果您感兴趣,代码为here
  • 这就是为什么我现在转向VisPy。

我正在使用类Visualizer来显示结果,并使用方法app.Timer().connect来管理实时端。 Pandemic代码为here

from Pandemic import *
from vispy.plot import Fig
from vispy import app

class Visualizer:
    def __init__(self, world):
        self.fig = Fig()
        self.world = world
        self.traces = {}

        #Scatter plots
        for idx, c in world.countries.items():
            pos_x = idx % self.world.nb_cols
            pos_y = idx // self.world.nb_cols
            subplot = self.fig[pos_y, pos_x]
            data = np.array([c.x_coord, c.y_coord]).reshape(-1,2)
            self.traces[idx] = subplot.plot(data, symbol='o', width=0, face_color=c.p_colors, title='Country {}'.format(idx+1))

    def display(self): 
        for idx, c in self.world.countries.items():
            data = np.array([c.x_coord, c.y_coord]).reshape(-1,2)
            self.traces[idx].set_data(data, face_color=c.p_colors)

    def update(self, event):
        self.world.update(quarantine=False)
        self.display()

    def animation(self):
        self.timer = app.Timer()
        self.timer.connect(self.update)
        self.timer.start(0)
        self.start()

    def start(self):
        if (sys.flags.interactive != 1):
            self.status = app.run()


if __name__ == '__main__':
    w = World(move=0.001)
    for i in range(8):
        w.add_country(nb_S=500)
    v = Visualizer(w)
    v.animation()

与散景一样,散点图每次其粒子变色时都会“闪烁”。难道我做错了什么?

是否有更有效的实时显示方式,也许使用vispy.gloo或vispy.scene? (暂时比pyqtgraph.opengl慢)

我写了一个脚本来模拟大流行的演变(带有图形和散点图)。我尝试了几个库来实时显示结果(8个国家x 500个粒子):Matplotlib(not ...

python plot real-time scatter-plot vispy
1个回答
0
投票

我们可以使用vispy.gloo模块来利用GPU的强大功能,从而实时高效地进行绘图。这是一种方法:

© www.soinside.com 2019 - 2024. All rights reserved.