4
$\begingroup$

I am using Mathematica 11

Compare below two timings

Table[AbsoluteTiming[ListPlot[RandomReal[1., {n, 2}]]][[1]], {n, 1, 
  200, 20}]
(*{0.0922182, 0.0868473, 0.0458226, 0.0350156, 0.0440754, 0.0346732, \
0.0368516, 0.0397308, 0.0528617, 0.0459663}*)

and

Table[AbsoluteTiming[
   ListPlot[Thread[RandomReal[1., {n, 2}] -> Range[n]]]][[1]], {n, 1, 
  200, 20}]
(*{0.445452, 0.621644, 1.20737, 2.16653, 3.06852, 7.26112, 12.347, \
17.6615, 23.6164, 29.5285}*)

Why plotting labeled points is so slow? Is there workaround?


update

Thanks to rcollyer. The slowness is due to auto position algorithm gone crazy when number of points get larger.

A workaround is to specify label position explicitly, for example

Table[AbsoluteTiming[
   ListPlot[
    Labeled[#[[1]], #[[2]], Below] & /@ 
     Thread[RandomReal[1., {n, 2}] -> Range[n]]]][[1]], {n, 1, 200, 
  20}]
(*{0.201465, 0.359929, 0.470717, 0.415873, 0.556001, 0.644012, \
0.707094, 0.945657, 0.874655, 1.12489}*)
$\endgroup$
8
  • 7
    $\begingroup$ ListPlot without labels is relatively straightforward to do, but adding labels and attempting to have them not overlap each other, is not. In fact, it is probably NP Hard. So, what you're seeing is the effects of the positioning algorithm go crazy because it can't comply. $\endgroup$
    – rcollyer
    Commented Oct 21, 2016 at 14:15
  • $\begingroup$ @rcollyer I thought it maybe a bug. But your comment makes sense : ) $\endgroup$
    – matheorem
    Commented Oct 21, 2016 at 14:19
  • $\begingroup$ ehm, is Labeled supposed to be in the second example? Otherwise comparing the two doesn't show us that Labeled is slow... $\endgroup$
    – C. E.
    Commented Oct 22, 2016 at 1:33
  • $\begingroup$ @C.E. because coordinate->label is dealt by ListPlot as labeled point. So I thought they are equivalent $\endgroup$
    – matheorem
    Commented Oct 22, 2016 at 1:35
  • 1
    $\begingroup$ @rcollyer Might I trouble you to post that as an Answer? $\endgroup$
    – Mr.Wizard
    Commented Oct 22, 2016 at 11:26

1 Answer 1

3
$\begingroup$

As I mentioned above, the problem is in positioning the labels. Without the labels present, ListPlot does not have to do much but parse the data and determine appropriate PlotRange. The addition of labels, though, adds a conundrum: how do we place the labels in a pleasing manner with minimal overlap? This is not straightforward, and as the number of labeled points increases, it becomes even harder to accomplish. As you pointed out, the simplest method is to bypass the automatic placement algorithm, e.g (borrowing your code shamelessly)

Table[First@AbsoluteTiming@
   ListPlot[Labeled[#[[1]], #[[2]], Below] & /@ 
     Thread[RandomReal[1., {n, 2}] -> Range[n]]]
  , {n, 1, 200, 20}
]
(*{0.201465, 0.359929, 0.470717, 0.415873, 0.556001, 0.644012, 
 0.707094, 0.945657, 0.874655, 1.12489}*)

The key thing to note here is that while it scales much better than the automatic algorithm, it does take quite a bit of extra time relative to no labels. So, after a certain point, you should look at only labeling specific points.

$\endgroup$

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.