简体   繁体   中英

Create a barplot using vispy Rectangle

I'm tryng to create a barchart using Vispy since im tended to use opengl cause of a large set of data.

From the vispy docs i found the visuals.Rectangle class which provides me a "bar". Unfortunately the class takes only 1 set of points so i tried to create multiple rectangles within a loop. At this point, the plot already slow down at a few rectangles, 100-1000 is already really bad. Obviously thats the wrong way to create the chart which brings me here. How can i create multiple rectangle without adding every rectangle separatley?

Heres a snippet:

import sys
import numpy as np
from vispy import scene, app

canvas = scene.SceneCanvas(keys='interactive')
canvas.size = 600, 600
canvas.show()

grid = canvas.central_widget.add_grid()
view = grid.add_view(row=0, col=0)
view.camera = scene.PanZoomCamera(rect=(-1,-1,10,10))

for i in range(10):
    rec = scene.visuals.Rectangle(center=(i, i), height=1, width=0.5, color='r')
    view.add(rec)
gl = scene.visuals.GridLines(parent=view.scene)

if __name__ == '__main__' and sys.flags.interactive == 0:
    app.run()

As you've discovered, there is no builtin bar chart for VisPy (it would be a welcome contribution to the plotting API). I think the easiest way to do it would be to make a MeshVisual that contains multiple rectangles. I don't have the code to do this, but this would be the starting point.

Generally, it is not recommended to create a lot of Visuals as it will hurt performance. VisPy (and OpenGL) will draw each visual serially, one at a time, and having a lot can really hurt your frames per second and overall performance.

Edit: Ah you could probably base this off of the HistogramVisual: https://github.com/vispy/vispy/blob/master/vispy/visuals/histogram.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM