|
From: Pellegrini E. <eri...@ya...> - 2006-12-06 08:32:22
|
Hi everybody,
I would like to build an application where many filled polygons will have to be displayed on the screen. To do so, I would like to use matplotlib but, up to now, my application is not fast enough.
I use:
python 2.4.1
matplot-0.87.6
numpy-1.0rc1
and I checked my matplotlibrc file, the interactive flag is False.
Here is a little script that illustrates my problem. It takes something like 5-10s to display 80 polygons on the screen using a brand new machine.
###########################################
import pylab
x = range(0,810,10)
pylab.axis('off')
for i in range(0,len(x)-1):
pylab.fill([x[i],x[i+1],x[i+1],x[i]],[10,10,20,20])
pylab.axis((0,810,0,610))
########################################
would you have any idea of what is going on or of a better way to implement this ?
Thank you very much
Eric
---------------------------------
Découvrez une nouvelle façon d'obtenir des réponses à toutes vos questions ! Profitez des connaissances, des opinions et des expériences des internautes sur Yahoo! Questions/Réponses. |
|
From: John H. <jdh...@ac...> - 2006-12-06 16:10:08
|
>>>>> "Pellegrini" == Pellegrini Eric <eri...@ya...> writes:
Pellegrini> Hi everybody, I would like to build an application
Pellegrini> where many filled polygons will have to be displayed
Pellegrini> on the screen. To do so, I would like to use
Pellegrini> matplotlib but, up to now, my application is not fast
Pellegrini> enough.
Hmm, I'm not seeing the performance problem on my system -- I can
create and save the figure in a fraction of a second. Is your numerix
setting set to numpy? Run the script with --verbose-helpful and send
us the output.
A few things to note: if you really want regular polygons, eg the
squared in your example, do any of the plot markers work for you.
plot(x, marker='s')
will be about as fast as mpl gets. You can set the marker size with
the markersize property.
Second, if you need arbitrary polygons, and need a lot of them, a
polygon collection will be faster. I had to bump the number of polys
up to about 8000 to show a dramatic performance difference.
Here are two scripts and performance numbers -- one using fill and one
using a polygon collection
> time python test.py -dAgg
6.595u 0.089s 0:06.68 99.8% 0+0k 0+0io 0pf+0w
> time python test2.py -dAgg
0.565u 0.033s 0:00.59 100.0% 0+0k 0+0io 0pf+0w
> cat test.py
import pylab
x = range(0,81000,10)
pylab.axis('off')
for i in range(0,len(x)-1):
pylab.fill([x[i],x[i+1],x[i+1],x[i]],[10,10,20,20])
pylab.axis((0,max(x),0,610))
pylab.savefig('test')
pylab.show()
> cat test2.py
import pylab
from matplotlib.collections import PolyCollection
fig = pylab.figure()
ax = fig.add_subplot(111)
x = range(0,81000,10)
pylab.axis('off')
verts = [((x[i], 10), (x[i+1], 10), (x[i+1], 20), (x[i], 20)) for i in range(len(x)-1)]
col = PolyCollection(verts)
ax.add_collection(col)
pylab.axis((0,max(x),0,610))
pylab.savefig('test')
pylab.show()
|
|
From: Pellegrini E. <eri...@ya...> - 2006-12-07 09:11:54
|
Hi John,
thank you very much for the hand.
I think that I have found my mistake. I was launching my script trough "Idle" that seems to be the reason why it was to slow. Running my script with the command line or by double-clicking on it gave results similar to yours. Would you know why Idle application slows down so much the execution of my script ?
By the way, using the Collection class, would you have any idea how to set different colors to the polygons ? Using set_color method change the color of all the plygons.
Thanks again
Eric
John Hunter <jdh...@ac...> a écrit :
>>>>> "Pellegrini" == Pellegrini Eric writes:
Pellegrini> Hi everybody, I would like to build an application
Pellegrini> where many filled polygons will have to be displayed
Pellegrini> on the screen. To do so, I would like to use
Pellegrini> matplotlib but, up to now, my application is not fast
Pellegrini> enough.
Hmm, I'm not seeing the performance problem on my system -- I can
create and save the figure in a fraction of a second. Is your numerix
setting set to numpy? Run the script with --verbose-helpful and send
us the output.
A few things to note: if you really want regular polygons, eg the
squared in your example, do any of the plot markers work for you.
plot(x, marker='s')
will be about as fast as mpl gets. You can set the marker size with
the markersize property.
Second, if you need arbitrary polygons, and need a lot of them, a
polygon collection will be faster. I had to bump the number of polys
up to about 8000 to show a dramatic performance difference.
Here are two scripts and performance numbers -- one using fill and one
using a polygon collection
> time python test.py -dAgg
6.595u 0.089s 0:06.68 99.8% 0+0k 0+0io 0pf+0w
> time python test2.py -dAgg
0.565u 0.033s 0:00.59 100.0% 0+0k 0+0io 0pf+0w
> cat test.py
import pylab
x = range(0,81000,10)
pylab.axis('off')
for i in range(0,len(x)-1):
pylab.fill([x[i],x[i+1],x[i+1],x[i]],[10,10,20,20])
pylab.axis((0,max(x),0,610))
pylab.savefig('test')
pylab.show()
> cat test2.py
import pylab
from matplotlib.collections import PolyCollection
fig = pylab.figure()
ax = fig.add_subplot(111)
x = range(0,81000,10)
pylab.axis('off')
verts = [((x[i], 10), (x[i+1], 10), (x[i+1], 20), (x[i], 20)) for i in range(len(x)-1)]
col = PolyCollection(verts)
ax.add_collection(col)
pylab.axis((0,max(x),0,610))
pylab.savefig('test')
pylab.show()
---------------------------------
Découvrez une nouvelle façon d'obtenir des réponses à toutes vos questions ! Profitez des connaissances, des opinions et des expériences des internautes sur Yahoo! Questions/Réponses. |
|
From: John H. <jdh...@ac...> - 2006-12-07 15:49:23
|
>>>>> "Pellegrini" == Pellegrini Eric <eri...@ya...> writes:
Pellegrini> Hi John, thank you very much for the hand.
Pellegrini> I think that I have found my mistake. I was
Pellegrini> launching my script trough "Idle" that seems to be the
Pellegrini> reason why it was to slow. Running my script with the
Pellegrini> command line or by double-clicking on it gave results
Pellegrini> similar to yours. Would you know why Idle application
Pellegrini> slows down so much the execution of my script ?
Probably because you are running in "interactive" mode and matplotlib
is redrawing your figure on every fill command. See
http://matplotlib.sf.net/interactive.html for details. You can
temporarily turn interaction on and off with the ion and ioff
functions. Something like
ioff()
for x in somerange():
fill(...)
ion()
Then matplotlib will turn off drawing for the loop.
Pellegrini> By the way, using the Collection class, would you
Pellegrini> have any idea how to set different colors to the
Pellegrini> polygons ? Using set_color method change the color of
Pellegrini> all the plygons.
You can pass in a sequence of facecolors the length of your number of
polygons. Each element of the sequence must be RGBA, but you can use
matplotlib's color converter to convert an arbitrary color argument to
RGBA.
from colors import colorConverter
colors = [colorConverter.to_rgba(x) for x in ('red', 'green', 'black', 'y', '0.8')]
collection = PolyCollection(verts, facecolors = colors)
You can also use colormapping with a PolyCollection where "c" below is an array of scalar intensities and cmap is a matplotlib.cm colormap, eg cm.jet and norm
collection.set_array(asarray(c))
collection.set_cmap(cmap)
Collections are covered at
http://matplotlib.sf.net/matplotlib.collections.html and in the user's
guide PDF on the web site.
JDH
|
|
From: Eric F. <ef...@ha...> - 2006-12-07 18:23:09
|
John Hunter wrote: [...] > You can pass in a sequence of facecolors the length of your number of > polygons. Each element of the sequence must be RGBA, but you can use > matplotlib's color converter to convert an arbitrary color argument to > RGBA. > John, You don't need to do this explicit conversion any more; some time ago I made it automatic, so you can pass in directly any sort of colorspec or colorspec list. Eric |
|
From: Pellegrini E. <eri...@ya...> - 2006-12-07 09:29:22
|
I forgot to send the output of the python test.py --verbose-helpful. Perhaps you will find something wrong there. Here it is:
################################"
matplotlib data path C:\Python24\lib\site-packages\matplotlib\mpl-data
$HOME=C:\Documents and Settings\Eric
CONFIGDIR=C:\Documents and Settings\Eric\.matplotlib
loaded rc file C:\Python24\lib\site-packages\matplotlib\mpl-data\matplotlibrc
matplotlib version 0.87.6
verbose.level helpful
interactive is False
platform is win32
numerix numpy 1.0rc1
font search path ['C:\\Python24\\lib\\site-packages\\matplotlib\\mpl-data']
loaded ttfcache file C:\Documents and Settings\Eric\.matplotlib\ttffont.cache
backend TkAgg version 8.4
########################################"
Thanks again
Eric
John Hunter <jdh...@ac...> a écrit :
>>>>> "Pellegrini" == Pellegrini Eric writes:
Pellegrini> Hi everybody, I would like to build an application
Pellegrini> where many filled polygons will have to be displayed
Pellegrini> on the screen. To do so, I would like to use
Pellegrini> matplotlib but, up to now, my application is not fast
Pellegrini> enough.
Hmm, I'm not seeing the performance problem on my system -- I can
create and save the figure in a fraction of a second. Is your numerix
setting set to numpy? Run the script with --verbose-helpful and send
us the output.
A few things to note: if you really want regular polygons, eg the
squared in your example, do any of the plot markers work for you.
plot(x, marker='s')
will be about as fast as mpl gets. You can set the marker size with
the markersize property.
Second, if you need arbitrary polygons, and need a lot of them, a
polygon collection will be faster. I had to bump the number of polys
up to about 8000 to show a dramatic performance difference.
Here are two scripts and performance numbers -- one using fill and one
using a polygon collection
> time python test.py -dAgg
6.595u 0.089s 0:06.68 99.8% 0+0k 0+0io 0pf+0w
> time python test2.py -dAgg
0.565u 0.033s 0:00.59 100.0% 0+0k 0+0io 0pf+0w
> cat test.py
import pylab
x = range(0,81000,10)
pylab.axis('off')
for i in range(0,len(x)-1):
pylab.fill([x[i],x[i+1],x[i+1],x[i]],[10,10,20,20])
pylab.axis((0,max(x),0,610))
pylab.savefig('test')
pylab.show()
> cat test2.py
import pylab
from matplotlib.collections import PolyCollection
fig = pylab.figure()
ax = fig.add_subplot(111)
x = range(0,81000,10)
pylab.axis('off')
verts = [((x[i], 10), (x[i+1], 10), (x[i+1], 20), (x[i], 20)) for i in range(len(x)-1)]
col = PolyCollection(verts)
ax.add_collection(col)
pylab.axis((0,max(x),0,610))
pylab.savefig('test')
pylab.show()
---------------------------------
Découvrez une nouvelle façon d'obtenir des réponses à toutes vos questions ! Profitez des connaissances, des opinions et des expériences des internautes sur Yahoo! Questions/Réponses. |