6
$\begingroup$

I have a list of points plotted using the command:

NumberLinePlot[{1, 1.5, 2}, PlotStyle -> {{Thick, Red, PointSize[Large]}, {Thick, Darker[Green], 
    PointSize[Large]}, {Thick, Blue, PointSize[Large]}}, 
 PlotTheme -> "Scientific"]

The output is of the form:

enter image description here

How can I label each point in the graph. Say, for instance, the red term is the first term in a sequence, the green term is the second term in the sequence etc. How can I label the plot such that this labelling, as $x_1, x_2$ is reflected in the graph. It'd be great if someone could suggest a way where I can introduce these labels directly on the graph, and not in the legend, so I don't necessarily have to color the points differently.

$\endgroup$
6
$\begingroup$

Post-process to add labels:

labels={"label1","label2","label3"};
i=1;
NumberLinePlot[{1,1.5, 2}, PlotStyle-> {{Thick, Red, PointSize[Large]},
  {Thick, Darker[Green], PointSize[Large]}, {Thick, Blue, PointSize[Large]}}, 
 ImagePadding -> 20] /. Point[x_]->{Point[x], Text[labels[[i++]], {0,1} + x]}

Mathematica graphics

Update: Stealing @Mr.Wizard's idea of using Tooltips with an alternative way to post-process

nlp = NumberLinePlot[Tooltip@@@Transpose[{{1,1.5, 2}, labels}], 
  PlotStyle -> {{Thick, Red, PointSize[Large]}, {Thick, Darker[Green], 
    PointSize[Large]}, {Thick, Blue, PointSize[Large]}}];

Show[nlp /. Tooltip[x_, y_] :> {x, Text[y, {0,1} + x[[1,1]]]}, ImagePadding -> 20]

Mathematica graphics

Update 2: Neither of the methods above work in version 11.3. The following modification of the first method works:

labels = {"label1", "label2", "label3"};
i = 1;
NumberLinePlot[List /@ {1, 1.5, 2}, 
  PlotStyle -> ({{Thick, Red, PointSize[Large]},
       {Thick, Darker[Green], PointSize[Large]}, {Thick, Blue, 
      PointSize[Large]}}), 
   ImagePadding -> 20, Spacings -> {1/4, 0, 0}] /. 
 Point[x_] :> {Point[x], Text[labels[[i++]], {0, 1/4} + x]}

enter image description here

$\endgroup$
  • $\begingroup$ I was about to note the example in the ListPlot documentation, but you got there first. $\endgroup$ – Titus Nov 16 '18 at 16:24
4
$\begingroup$

Tooltip gives you hover-over labels:

dat = {Tooltip[1, "x1"], Tooltip[1.5, "x2"], Tooltip[2, "x3"]};

p1 = NumberLinePlot[dat, 
 PlotStyle -> {{Thick, Red, PointSize[Large]}, {Thick, Darker[Green], 
    PointSize[Large]}, {Thick, Blue, PointSize[Large]}}, PlotTheme -> "Scientific"]

I am working on always-visible labels now.


I could not quickly trace the handling of Labeled in NumberLinePlot but it seems it is not supported. We could extract points and re-plot them with a function that does support Labeled, ListPlot:

pts = Cases[p1, Tooltip[{_[pt_]}, lbl_] :> Labeled[pt, lbl], -3];

Show[p1, ListPlot[pts, PlotStyle -> None], PlotRange -> All]

enter image description here

$\endgroup$
  • $\begingroup$ Oh. But if I want to embed the graph in a PDF? For that, ideally, I was hoping there's a way I could label the points on the numberline and make that labelling apparent. $\endgroup$ – Junaid Aftab Mar 8 '17 at 3:39
  • $\begingroup$ @JunaidAftab Yes, I understand. Please see my updated answer. This uses the automatically positioned labeling of ListPlot in an overlay of the NumberLinePlot. $\endgroup$ – Mr.Wizard Mar 8 '17 at 4:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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