如何用VisPy库实时绘图? [英] How to plot in real time with VisPy library?

查看:26
本文介绍了如何用VisPy库实时绘图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个脚本,用于对大流行的演变进行建模(使用图表和散点图)。 我尝试了几个库来实时显示结果(8个国家/地区x 500个粒子):

  • Matplotlib(不够快)
  • PyQtGraph(更好,但仍不够快)
  • OpenGL(很好,但我不知道如何在2D中有效地使用它,使用子情节、标题、图例...)
  • Bokeh(很好,但散布图每次粒子变色时都会"闪烁"。如果您感兴趣,代码为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()

散点图每次粒子变色时都会"闪烁",就像Bokeh一样。我做错了什么吗?

有没有更有效的实时显示方式,比如使用vispy.glo或vispy.Scene?(目前比pyqtgraph.opengl慢)

推荐答案

我们可以通过vispy.gloo模块利用图形处理器的能力,高效地实时绘图。以下是一种方法:

1)生成继承vispy.app.Canvas类的类。

2)创建输入为着色器的OpenGL程序。此对象允许我们将数据链接到着色器变量。画布上的每个点取决于这些变量值(描述其坐标、颜色等)。例如,显示文本(标题、标签等)要比使用Matplotlib库困难得多。Here对该过程进行了更深入的说明。

3)设置连接到我们要重复调用的函数的计时器(实时端)。

vispy.scene模块专用于科学家的高级可视化界面,目前仍处于实验阶段。也许这就是我的第一个代码出现错误的原因。

Here是我的新代码。

这篇关于如何用VisPy库实时绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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