Ask Your Question
0

Improve time to plot a set of points

asked 2024-02-09 12:14:03 +0100

Emm gravatar image

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.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2024-02-09 20:33:15 +0100

Emmanuel Charpentier gravatar image

Your 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,

edit flag offensive delete link more

Comments

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

Emm gravatar imageEmm ( 2024-02-11 12:30:04 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2024-02-09 12:14:03 +0100

Seen: 167 times

Last updated: Feb 09