我写了一个脚本来模拟大流行的演变(带有图形和散点图)。我尝试了几个库来实时显示结果(8个国家x 500个粒子):
这就是为什么我现在转向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 ...
我们可以使用vispy.gloo
模块来利用GPU的强大功能,从而实时高效地进行绘图。这是一种方法: