plotting slows down

N

norman.elliott

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)
 
D

Dave Angel

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 *[/QUOTE]

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
 
N

Norman Elliott

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)
 
I

Ian Kelly

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.
 
C

Chris Angelico

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.

ChrisA
 
I

Ian Kelly

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.
 
D

Dave Angel

Chris Angelico said:
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'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.

--
DaveA



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

Norman Elliott

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)

and added
Code:
update()

immediately after the line
Code:
for y in range(1,12):

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

Steven D'Aprano

(e-mail address removed) Wrote in message:
Code:
#!/usr/bin/python
from graphics import *[/QUOTE]

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.[/QUOTE]

He did. If you look at the original message as posted to python-
[email protected] 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.
[QUOTE]
Your message shows it all flushed to the left margin,  probably due to
posting in html mode. Use text mode here.[/QUOTE]

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

Terry Reedy

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.
 
D

Dave Angel

Steven D'Aprano said:
(e-mail address removed) Wrote in message:
Code:
#!/usr/bin/python
from graphics import *[/QUOTE]

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.[/QUOTE]

He did. If you look at the original message as posted to python-
[email protected] 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.
[QUOTE]
Your message shows it all flushed to the left margin,  probably due to
posting in html mode. Use text mode here.[/QUOTE]

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

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.


--
DaveA



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

Norman Elliott

@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.
 
R

Rustom Mody

@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.
 
C

Chris Angelico

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
 
D

Dennis Lee Bieber

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.

Perhaps even more than you think... Your responses are showing up here
with a 1-space hanging indent; as if it considers the space when
word-wrapping to be the beginning of a word, rather than the end of a word.
 
N

Norman Elliott

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)

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top