Ask Your Question

Improve time to plot a set of points

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

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():
    for p in P:
def Trace2():

(*) Replacing




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

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

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: P2.parent()
<class ''>
sage: P3.parent()
<class ''>
sage: len(P1)
sage: len(P2)
sage: len(P3)


edit flag offensive delete link more


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 +0200 )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


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

Seen: 86 times

Last updated: Feb 09