I have a very big list of numbers in a file. They represent locations of pool balls on board over time while they move.

There are $N$ balls, and the points are arrenged such that every $N$ terms on the list are the positions balls at a certain time.

For example, for 2 balls the list:

1 1 1 2 1 1.5 1 2 1 2 1 2.5

Means that the positions of the balls are:

t=t1: ball 1 = {1,1}; ball 2 = {1,2}
t=t2: ball 1 = {1,1.5}; ball 2 = {1,2}
t=t3: ball 1 = {1,2}; ball 2 = {1,2.5}

I want to visualize this list on wolfram mathematica. My question is, what is the most efficient way to do it?

(Of course, my list is a lot bigger then the example list with a lot more points. That means, efficiency is critical for me.)

Previously, I used to get the points as a list into Mathematica:

pos := ReadList["LOCATION OF THE FILE", {Number, Number}]

and then used Graphics to show each time 2 points of the list:

numberOfBalls := N;
    {Hue[(2 - k)/numberOfBalls], EdgeForm[Thick], 
     Disk[Reverse[pos[[m + k]]], 0.0265], Black, 
     Text[Reverse[pos[[m + k]]], {0, 0.06} + Reverse[pos[[m + k]]]]}
    , {k, 0, numberOfBalls - 1}]]
 , {m, 1, Length[pos] - 1, numberOfBalls}]

But I'm sure that there is a better and more efficient way to do this.

The link to the list is here: (With 3 balls)


Thanks a lot!


1 Answer 1


Use Partition:

data = First@Import["~/Downloads/wolfram.txt", "Table"];
numberOfBalls = 3;
pos = Partition[Partition[data, 2], numberOfBalls];
Animate[ListPlot[pos[[i]], PlotRange -> {{-.7, .7}, {-1.5, 1.5}}], 
         {i, 1, Length@pos, 1}]

For a nice animation, you'd have to take into account the balls diameters, but that's another problem.

Edit Using Graphics instead of ListPlot for more efficiency:

Animate[Graphics[Point @ pos[[i]], PlotRange -> {{-.7, .7}, {-1.5, 1.5}}], 
          {i, 1, Length@pos, 1}]
  • $\begingroup$ Well that worked like magic! Thanks! $\endgroup$
    – Yoav Zack
    Mar 27, 2018 at 19:59
  • $\begingroup$ I would use Graphics directly, avoid the overhead of ListPlot. $\endgroup$
    – Carl Woll
    Mar 27, 2018 at 21:05
  • $\begingroup$ @CarlWoll I read in a recent answer you wrote that Graphics was faster. When would you use ListPlot then? When you want to manage different curve styles, etc.? $\endgroup$
    – anderstood
    Mar 27, 2018 at 21:07
  • 1
    $\begingroup$ Yes, ListPlot is useful for those kinds of things, as well as some others like Filling. Note that Point supports multiple points, so using Point[pos[[i]]] is also possible, and faster if there are a lot of points. $\endgroup$
    – Carl Woll
    Mar 27, 2018 at 21:16
  • 1
    $\begingroup$ As Carl says, if you don't need the fancy bits like filling, custom symbols for the points, or automagically removing Missing[] entries, then Graphics[] has way less overhead than ListPlot[]. $\endgroup$ Mar 28, 2018 at 1:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.