Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiently Plotting Many Lines in VisPy

From all example code/demos I have seen in the VisPy library, I only see one way that people plot many lines, for example:

for i in range(N):
    pos = pos.copy()
    pos[:, 1] = np.random.normal(scale=5, loc=(i+1)*30, size=N)
    line = scene.visuals.Line(pos=pos, color=color, parent=canvas.scene)
    lines.append(line)
canvas.show()

My issue is that I have many lines to plot (each several hundred thousand points). Matplotlib proved too slow because of the total number of points plotted was in the millions, hence I switched to VisPy. But VisPy is even slower when you plot thousands of lines each with thousands of points (the speed-up comes when you have millions of points).

The root cause is in the way lines are drawn. When you create a plot widget and then plot a line, each line is rendered to the canvas. In matplotlib you can explicitly state to not show the canvas until all lines are drawn in memory, but there doesn't appear to be the same functionality in VisPy, making it useless.

Is there any way around this? I need to plot multiple lines so that I can change properties interactively, so flattening all the data points into one plot call won't work.

(I am using a PyQt4 to embed the plot in a GUI. I have also considered pyqtgraph.)

like image 245
Max Kanwal Avatar asked Nov 05 '25 13:11

Max Kanwal


1 Answers

You should pass an array to the "connect" parameter of the Line() function.

    xy = np.random.rand(5,2) # 2D positions
    # Create an array of point connections :
    toconnect = np.array([[0,1], [0,2], [1,4], [2,3], [2,4]])
    # Point 0 in your xy will be connected with 1 and 2, point 
    # 1 with 4 and point 2 with 3 and 4.
    line = scene.visuals.Line(pos=xy, connect=toconnect)

You only add one object to your canvas but the control pear line is more limited.

like image 50
Etienne Cmb Avatar answered Nov 09 '25 10:11

Etienne Cmb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!