Plotting large number of lines

205 views
Skip to first unread message

Bartosz

unread,
Feb 10, 2012, 6:50:50 AM2/10/12
to glumpy-users, belev...@gmail.com
Hi all,

I am trying to plot large number of lines in 2d (about 40k lines 50
points each). Unfortunately, matplotlib and other libraries are very
slow at plotting large dataset. My initial benchmarks of plotting with
PyOpenGL showed that it can be more than 10 times faster even for
simple 2d graphs.

Then I found glumpy and as far as I understand it tries to solve very
similar problem. It would be great if we could use it for our project.
Is it possible to plot line graphs in glumpy? If not, what would be
necessary to implement such a feature?

Here is my benchmark PyOpenGL code: https://gist.github.com/1716954#file_test_opengl.py
. (if you scroll up/down you will find alternative - slower -
implementations). Please take a look at it, if you want to know
exactly what I have in mind.

Yours,

Bartosz

Nicolas Rougier

unread,
Feb 10, 2012, 10:33:02 AM2/10/12
to glumpy-users


There is not such 2d graphs even if you can use glumpy window to draw
anything you want. In the previous glumpy version, there was a script
showing how to integrate matplotlib and glumpy and I need to put it
back into the examples. That may be the best solution in your case,
all the fancy stuff is done by matplotlib and the fast stuff is done
using glumpy.

I've looked at your code and I think your OpenGL is quite efficient.
You can speed it up a little bit by discarding depth buffer as shown
below.

Nicolas



import sys
import numpy as np
import OpenGL
OpenGL.ERROR_ON_COPY = True
import OpenGL.GL as gl
import OpenGL.GLUT as glut

frame, timebase = 0, 0

def display():
global frame, timebase

frame = frame+1
time = glut.glutGet( glut.GLUT_ELAPSED_TIME )
if (time - timebase > 1000):
fps = frame*1000.0/(time-timebase)
print fps
timebase,frame = time,0

gl.glClearColor(0,0,0,1)
gl.glEnable(gl.GL_BLEND)
gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)


gl.glClear(gl.GL_COLOR_BUFFER_BIT)
x,y,width,height = gl.glGetIntegerv(gl.GL_VIEWPORT)

# n = number of lines, p = points per line
n,p = 5000, 50
vertices = np.zeros((n,p,2),dtype=np.float32)
vertices[:,:,0] = np.random.uniform(0,width,n*p).reshape(n,p)
vertices[:,:,1] = np.random.uniform(0,height,n*p).reshape(n,p)
gl.glColor(1, 1, 1, 0.05)
gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
gl.glVertexPointer(2, gl.GL_FLOAT, 0, vertices)
for i in xrange(n):
gl.glDrawArrays(gl.GL_LINE_STRIP, i*p, p)
gl.glDisableClientState(gl.GL_VERTEX_ARRAY);
glut.glutSwapBuffers()

def reshape(width,height):
gl.glViewport(0, 0, width, height)
gl.glMatrixMode(gl.GL_PROJECTION)
gl.glLoadIdentity()
gl.glOrtho(0, width, 0, height, -1, 1)
gl.glMatrixMode(gl.GL_MODELVIEW)

def keyboard( key, x, y ):
if key == '\033': sys.exit( )

if __name__ == '__main__':
glut.glutInit(sys.argv)
glut.glutInitDisplayMode(glut.GLUT_DOUBLE | glut.GLUT_RGBA)
glut.glutCreateWindow('glut-lines')
glut.glutReshapeWindow(800,800)
glut.glutDisplayFunc(display)
glut.glutReshapeFunc(reshape)
glut.glutKeyboardFunc(keyboard )
glut.glutIdleFunc(display)
glut.glutMainLoop()

Bartosz Telenczuk

unread,
Feb 10, 2012, 10:46:40 AM2/10/12
to glumpy...@googlegroups.com, Дмитро Бєлєвцов
Thanks for reply and tips.

Do you think that 2d graphs could be included in glumpy? We do not actually need much fancy matplotlib stuff, such as ticks, axes and so on; just plotting the lines + zoom in/out.

Bartek

Nicolas Rougier

unread,
Feb 13, 2012, 1:39:02 AM2/13/12
to glumpy-users

I've started this some time ago and I will reconsider including it in
glumpy. It won't in the next fex days since I'm a bit busy until end
of February. I intend to update glumpy after that.


On Feb 10, 4:46 pm, Bartosz Telenczuk <bartosz.telenc...@gmail.com>
wrote:

Bartosz Telenczuk

unread,
Feb 13, 2012, 6:53:46 AM2/13/12
to glumpy...@googlegroups.com
Cool. Please keep me informed. I will be definitely interested to test it.

Bartosz

Дмитро Бєлєвцов

unread,
May 3, 2012, 3:43:18 PM5/3/12
to glumpy...@googlegroups.com
Hi,

I'm currently trying to port the Bartosz's example to glumpy. It runs, but I can't get the alpha blending to work. What could be the problem?


import numpy as np
import OpenGL.GL as gl
import glumpy

fig = glumpy.figure(size=(400,400))

n_pts = 34
n_lines = 50
t = np.linspace(-1, 1, n_pts)
y = np.sin(t*np.pi)

vertices = np.empty((n_lines, n_pts, 2), dtype=np.float)
vertices[:,:,0] = t[:, None].T
for i in xrange(n_lines):
    vertices[i,:,1] = y + (np.random.uniform(size=n_pts)-0.5)*0.5
vertices = vertices.flatten()

@fig.event
def on_draw():
    fig.clear()
    
    gl.glEnable(gl.GL_BLEND)
    gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
    
    gl.glMatrixMode(gl.GL_PROJECTION)
    gl.glLoadIdentity() 

    gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
    gl.glVertexPointer(2, gl.GL_FLOAT, 0, vertices)
    gl.glColor4f(1.0,0.0,0.0,0.1)
    
    for i in xrange(n_lines):
        gl.glDrawArrays(gl.GL_LINE_STRIP, i*n_pts, n_pts)     
    
    gl.glDisableClientState(gl.GL_VERTEX_ARRAY)

fig.show()

Cheers,
Dmytro


Понеділок, 13 лютого 2012 р. 13:53:46 UTC+2 користувач Bartosz написав:
Cool. Please keep me informed. I will be definitely interested to test it.

BartoszWhat

Дмитро Бєлєвцов

unread,
May 8, 2012, 4:53:57 AM5/8/12
to glumpy...@googlegroups.com
Ok, figured it out, the problem was that I didn't disable the depth buffer:

gl.glDisable(gl.GL_DEPTH_TEST)

Now it works just fine.

Четвер, 3 травня 2012 р. 22:43:18 UTC+3 користувач Дмитро Бєлєвцов написав:

Nicolas Rougier

unread,
Aug 17, 2012, 4:20:43 AM8/17/12
to glumpy...@googlegroups.com

I've added a link int the news to some experiment I'm currently doing for rendering lines and shapes. You can find it here:


and you might be interested in the very last screenshots that show pan/zoom with a bunch of lines.

Code is available from:


I intend to integrate it into glumpy at some point in the future but may the demo-lines is what you want.


Nicolas



Reply all
Reply to author
Forward
0 new messages