ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 11 Feb 2024 12:30:04 +0100Improve time to plot a set of pointshttps://ask.sagemath.org/question/75881/improve-time-to-plot-a-set-of-points/I try to draw clouds of coloured dots. The coordinates of the points are given by a formula. Let's say they're randomly distributed in [0,1]: that doesn't really matter here and isn't the problem.
For the plot, I use the following code. It's remarkably slow (in the sense that I didn't expect it to take so long).
The options don't seem to be the cause of the slowdown (*), but they do limit my use of list_plot, which seems to be much more efficient.
For example, with the following code:
- the total CPU time for Trace() is 1min
- the total CPU time for Trace2() is 121ms
I'm looking for ideas on how to **improve the time it takes to draw my colored dots** (I'd probably have to find a suitable python library, but I'm far from an expert...).
N = 10^4
R = range(N)
P=[(randrange(N)/N*1.0,randrange(N)/N*1.0) for k in R]
#..................
def mycolorscale(k):
return Color(1 - k/N, 0, 0)
#..................
def Trace():
g=Graphics()
k=0
for p in P:
g+=point(p,rgbcolor=mycolorscale(k),size=1)
k+=1
g.show()
#..................
def Trace2():
list_plot(P,plotjoined=False,size=1).show()
(*) Replacing
g+=point(p,rgbcolor=mycolorscale(k),size=1)
by
g+=point(p)
in the Trace code changes essentially nothing to the duration.
Fri, 09 Feb 2024 12:14:03 +0100https://ask.sagemath.org/question/75881/improve-time-to-plot-a-set-of-points/Answer by Emmanuel Charpentier for <p>I try to draw clouds of coloured dots. The coordinates of the points are given by a formula. Let's say they're randomly distributed in [0,1]: that doesn't really matter here and isn't the problem.</p>
<p>For the plot, I use the following code. It's remarkably slow (in the sense that I didn't expect it to take so long). </p>
<p>The options don't seem to be the cause of the slowdown (*), but they do limit my use of list_plot, which seems to be much more efficient.</p>
<p>For example, with the following code:</p>
<ul>
<li>the total CPU time for Trace() is 1min</li>
<li>the total CPU time for Trace2() is 121ms</li>
</ul>
<p>I'm looking for ideas on how to <strong>improve the time it takes to draw my colored dots</strong> (I'd probably have to find a suitable python library, but I'm far from an expert...).</p>
<pre><code>N = 10^4
R = range(N)
P=[(randrange(N)/N*1.0,randrange(N)/N*1.0) for k in R]
#..................
def mycolorscale(k):
return Color(1 - k/N, 0, 0)
#..................
def Trace():
g=Graphics()
k=0
for p in P:
g+=point(p,rgbcolor=mycolorscale(k),size=1)
k+=1
g.show()
#..................
def Trace2():
list_plot(P,plotjoined=False,size=1).show()
</code></pre>
<p>(*) Replacing </p>
<pre><code>g+=point(p,rgbcolor=mycolorscale(k),size=1)
</code></pre>
<p>by</p>
<pre><code>g+=point(p)
</code></pre>
<p>in the Trace code changes essentially nothing to the duration. </p>
https://ask.sagemath.org/question/75881/improve-time-to-plot-a-set-of-points/?answer=75898#post-id-75898Your `Trace` code creates a *list* of `N` graphical objects (one for each point), which implies `N-1` mergings of such objects. The alternative is to create *one* graphical object for all the points :
sage: time P1=points(list(tuple(u) for u in zip(range(5), range(5, 10))))
CPU times: user 68 µs, sys: 11 µs, total: 79 µs
Wall time: 81.3 µs
sage: time P2=list_plot(list(tuple(u) for u in zip(range(5), range(5, 10)))) # Variant
CPU times: user 83 µs, sys: 0 ns, total: 83 µs
Wall time: 84.9 µs
sage: time P3=sum([plot(point(tuple(u))) for u in zip(range(5), range(5, 10))])
CPU times: user 292 µs, sys: 0 ns, total: 292 µs
Wall time: 297 µs
sage: P1.parent()
<class 'sage.plot.graphics.Graphics'>
sage: P2.parent()
<class 'sage.plot.graphics.Graphics'>
sage: P3.parent()
<class 'sage.plot.graphics.Graphics'>
sage: len(P1)
1
sage: len(P2)
1
sage: len(P3)
5
HTH,Fri, 09 Feb 2024 20:33:15 +0100https://ask.sagemath.org/question/75881/improve-time-to-plot-a-set-of-points/?answer=75898#post-id-75898Comment by Emm for <p>Your <code>Trace</code> code creates a <em>list</em> of <code>N</code> graphical objects (one for each point), which implies <code>N-1</code> mergings of such objects. The alternative is to create <em>one</em> graphical object for all the points :</p>
<pre><code>sage: time P1=points(list(tuple(u) for u in zip(range(5), range(5, 10))))
CPU times: user 68 µs, sys: 11 µs, total: 79 µs
Wall time: 81.3 µs
sage: time P2=list_plot(list(tuple(u) for u in zip(range(5), range(5, 10)))) # Variant
CPU times: user 83 µs, sys: 0 ns, total: 83 µs
Wall time: 84.9 µs
sage: time P3=sum([plot(point(tuple(u))) for u in zip(range(5), range(5, 10))])
CPU times: user 292 µs, sys: 0 ns, total: 292 µs
Wall time: 297 µs
sage: P1.parent()
<class 'sage.plot.graphics.Graphics'>
sage: P2.parent()
<class 'sage.plot.graphics.Graphics'>
sage: P3.parent()
<class 'sage.plot.graphics.Graphics'>
sage: len(P1)
1
sage: len(P2)
1
sage: len(P3)
5
</code></pre>
<p>HTH,</p>
https://ask.sagemath.org/question/75881/improve-time-to-plot-a-set-of-points/?comment=75930#post-id-75930Thank you for your reply. However, I don't see how to change the color with the creation of a single graphical object, which is why I had to switch from my Trace2() function to my Trace() function.Sun, 11 Feb 2024 12:30:04 +0100https://ask.sagemath.org/question/75881/improve-time-to-plot-a-set-of-points/?comment=75930#post-id-75930