plotting slows down

Ian Kelly ian.g.kelly at gmail.com
Mon Jan 13 13:05:06 EST 2014


On Mon, Jan 13, 2014 at 1:15 AM,  <norman.elliott at gmail.com> wrote:
> First let me say I have not done much python programming!
> I am running Python 2.7.3.
> I am trying to use python as a front end to a simple oscilloscope.
> Ultimately I intend to use it with my micropython board.
>
> At the moment I am just developing it. All it does is use a module I found called graphics.py to create a window and display randomly generated data.
>
> Each time it goes through the outer loop it gets slower and slower.
> I put in a small delay just so I could observe what is happening and for the first line it draws it takes about a second. If I set it to loop 20 times the final loop takes more than 6 seconds.
> Can anyone explain what I am doing wrong please?

I wager the problem is in the "range(1, xpos)" inner loop.  Each time
this runs the win.plot() call adds a 1-pixel line to the underlying Tk
canvas.  These 1-pixel lines are never deleted, so they accumulate
over each outer loop.  Every time a new object is drawn, the canvas
has to process all of the lines that have been drawn in order to
redraw itself, and so it gets slower and slower.

One simple fix you might try to improve the rendering efficiency is to
disable the autoflush option documented here:

http://mcsp.wartburg.edu/zelle/python/graphics/graphics/node14.html

And then call the module-level update() function after each iteration
of the outer loop to force things to redraw.  In order to
realistically use this module for animation it looks like you will at
some point need to keep the number of graphics objects under some
constant.  To do this you could either reuse the existing objects by
calling their "move" method to reposition them as needed, or simply
remove them from the plot with the "undraw" method and draw new
objects in their place.  See:

http://mcsp.wartburg.edu/zelle/python/graphics/graphics/node3.html

If this still isn't fast enough for the number of objects you're
drawing, then you may just need to find a new drawing package, as this
one appears to be designed for teaching rather than efficiency.



More information about the Python-list mailing list