Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

plotting slows down

53 views
Skip to first unread message

norman....@gmail.com

unread,
Jan 13, 2014, 3:15:08 AM1/13/14
to
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?
Here is the code:
[code]
#!/usr/bin/python
from graphics import *
import random
import time

xpos=1200
ypos=400
ypnt=ypos/2
pos=1
#setBackground("white")
def main():
win = GraphWin("My Circle", xpos, ypos)
# win.setBackGround('white')
for y in range(1,5):
cir2 = Circle(Point(xpos/2,20), 10)
cir2.setFill("white")
cir2.draw(win)
message = Text(Point(win.getWidth()/2, 20), y)
message.draw(win)
j = random.randint(1,ypos)
for x in range(1,xpos):
updown = random.randint(0,1)
if updown:
j=j+1
else:
j=j-1
if j <1:
j=ypos/2
if j>ypos-1:
j=ypos/2
win.plot(x,j,"red")
time.sleep(.0001)

main()
time.sleep(5)
[/code]


Dave Angel

unread,
Jan 13, 2014, 8:26:11 AM1/13/14
to pytho...@python.org
norman....@gmail.com Wrote in message:
> 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?
> Here is the code:
> [code]
> #!/usr/bin/python
> from graphics import *

First things first. what operating system are you using, and
where did you get the mysterious graphics. py? Thanks for
telling us python 2.7.3

Next, please repost any source code with indentation preserved.
Your message shows it all flushed to the left margin, probably
due to posting in html mode. Use text mode here.

Finally, since you seem to be using googlegroups, please make
sure you don't double space your quotes. See. wiki.python.org/moi
n/GoogleGroupsPython

>

--
DaveA



----Android NewsGroup Reader----
http://www.piaohong.tk/newsgroup

Norman Elliott

unread,
Jan 13, 2014, 8:32:19 AM1/13/14
to

Norman Elliott

unread,
Jan 13, 2014, 8:45:03 AM1/13/14
to
I am running ubuntu 12.04 with all updates installed.

I got the graphics here:
http://mcsp.wartburg.edu/zelle/python/graphics/graphics/index.html

I cannot see how to change from html to text mode in chromium or within the group.

I read the link about double spacing so I will watch out for it.

Ian Kelly

unread,
Jan 13, 2014, 12:39:03 PM1/13/14
to Python
On Mon, Jan 13, 2014 at 6:26 AM, Dave Angel <da...@davea.name> wrote:
> Next, please repost any source code with indentation preserved.
> Your message shows it all flushed to the left margin, probably
> due to posting in html mode. Use text mode here.

That's odd, the message that I got includes proper indentation and is
plain text, not html.

Chris Angelico

unread,
Jan 13, 2014, 12:45:12 PM1/13/14
to Python
Also what I saw. Dave, do you get the newsgroup or the mailing list? I
get the mailing list - it's possible the HTML version got stripped by
Mailman.

ChrisA

Dave Angel

unread,
Jan 13, 2014, 1:05:52 PM1/13/14
to pytho...@python.org
Chris Angelico <ros...@gmail.com> Wrote in message:
I'm using gmane newsgroup, and recently switched to the Android
Newsgroup Reader. Previously was using Groundhog, which seemed to
eat the body of any message containing an html part. (Though
strangely it showed the footer in the tutor newsgroup). This one
was mentioned byAlan, and she far has seemed much
better.

Ian Kelly

unread,
Jan 13, 2014, 1:05:06 PM1/13/14
to Python
On Mon, Jan 13, 2014 at 1:15 AM, <norman....@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.

Norman Elliott

unread,
Jan 13, 2014, 1:33:43 PM1/13/14
to
On Monday, 13 January 2014 18:05:52 UTC, Dave Angel wrote:
> Chris Angelico Wrote in message:
>


Well Ian's suggestion has really done the job. Now each iteration takes just 0.14 seconds now.
changed to:
[code]
win = GraphWin("My Circle", xpos, ypos, autoflush=False)
[/code]

and added
[code]
update()
[/code]

immediately after the line
[code]
for y in range(1,12):
[/code]

previously the first iteration took 0.48 seconds and the the 10th 4.76

Steven D'Aprano

unread,
Jan 13, 2014, 3:33:11 PM1/13/14
to
On Mon, 13 Jan 2014 08:26:11 -0500, Dave Angel wrote:

> norman....@gmail.com Wrote in message:

>> [code]
>> #!/usr/bin/python
>> from graphics import *
>
> First things first. what operating system are you using, and
> where did you get the mysterious graphics. py? Thanks for telling us
> python 2.7.3
>
> Next, please repost any source code with indentation preserved.

He did. If you look at the original message as posted to python-
li...@python.org and comp.lang.python, e.g. here:

https://mail.python.org/pipermail/python-list/2014-January/664430.html

you will see that the message is correctly indented with tabs.

> Your message shows it all flushed to the left margin, probably due to
> posting in html mode. Use text mode here.

Looks like perhaps Gmane is stripping tabs from their mirror. You should
report that as a bug to them.



--
Steven

Terry Reedy

unread,
Jan 13, 2014, 4:42:18 PM1/13/14
to pytho...@python.org
On 1/13/2014 12:45 PM, Chris Angelico wrote:
> On Tue, Jan 14, 2014 at 4:39 AM, Ian Kelly <ian.g...@gmail.com> wrote:
>> On Mon, Jan 13, 2014 at 6:26 AM, Dave Angel <da...@davea.name> wrote:
>>> Next, please repost any source code with indentation preserved.
>>> Your message shows it all flushed to the left margin, probably
>>> due to posting in html mode. Use text mode here.
>>
>> That's odd, the message that I got includes proper indentation and is
>> plain text, not html.
>
> Also what I saw. Dave, do you get the newsgroup or the mailing list? I
> get the mailing list - it's possible the HTML version got stripped by
> Mailman.

I am reading via gmane. Viewing the source, there is no html.
BUT, indents are with tabs, not spaces. Some readers just delete tabs,
as there is no standard for conversion to spaces, especially with
proportional fonts. Thunderbird used to do this, but now uses tab stops
every 8 spaces (maybe because a switched to a fixed font?) This means
that the first tab gives an indent 8 chars in the original post, 6 in
the first quotation, and, I presume, 4 in a second quotation, etc. It
works better to post code with space indents.

--
Terry Jan Reedy

Dave Angel

unread,
Jan 14, 2014, 4:32:01 AM1/14/14
to pytho...@python.org
Norman Elliott <norman....@gmail.com> Wrote in message:
>
> I cannot see how to change from html to text mode in chromium or within the group.
>

You already did post in text mode, my error. The new newsreader
I'm using apparently eats tabs.

Dave Angel

unread,
Jan 14, 2014, 4:39:11 AM1/14/14
to pytho...@python.org
Steven D'Aprano <st...@pearwood.info> Wrote in message:
I just went to my Linux box and fired up thunderbird. When it
reads the same message from gmane, it gets the tabs. So
apparently it's this Android Newsgroups Reader that's buggy.

Norman Elliott

unread,
Jan 14, 2014, 8:04:43 AM1/14/14
to
@Dave, no problem. I am using gedit to write the files and have it set to translate tabs into 4 spaces which is what was recommended to me as the right amount of indenting for python scripts.

On Monday, 13 January 2014 08:15:08 UTC, Norman Elliott wrote:

Rustom Mody

unread,
Jan 14, 2014, 8:15:01 AM1/14/14
to
On Tuesday, January 14, 2014 6:34:43 PM UTC+5:30, Norman Elliott wrote:
> @Dave, no problem. I am using gedit to write the files and have it set to translate tabs into 4 spaces which is what was recommended to me as the right amount of indenting for python scripts.

Dunno what you mean by 'translate'
If that means actually replace the characters, then that will cause minimum problems

However it can also mean that gedit sets tabstops at 4 character intervals
Which will mean you will see 4 characters (in gedit) and everyone else will see a
tab. This is a recipe for trouble.

Chris Angelico

unread,
Jan 14, 2014, 8:36:34 AM1/14/14
to pytho...@python.org
On Wed, Jan 15, 2014 at 12:15 AM, Rustom Mody <rusto...@gmail.com> wrote:
> However it can also mean that gedit sets tabstops at 4 character intervals
> Which will mean you will see 4 characters (in gedit) and everyone else will see a
> tab. This is a recipe for trouble.

Not a recipe for trouble normally, it's just that some people's
clients can't see them. So keep using tabs if you want to, but be
prepared to search-and-replace them to spaces prior to posting code.
Though I think the fault is with the client(s) that can't see tabs,
and they're the ones that ought to be fixed.

ChrisA
Message has been deleted

Norman Elliott

unread,
Jan 14, 2014, 12:06:00 PM1/14/14
to
On Monday, 13 January 2014 08:15:08 UTC, Norman Elliott wrote:
Okay, maybe I misunderstood what it was doing. I have checked and I will do a find and replace of the tabs with 4 spaces in future.
0 new messages