3
$\begingroup$

How do I use LabelingFunction on a List of String Variables or a Function that generates String Variables? I want the List Plot to Display the individual elements of the List variable LabelString.

LabelString = Table[StringForm["Test # ``", LS], {LS, 1, 25}];

 ListPlot[Prime[Range[10]], LabelingFunction ->(LabelString[[#]] & ) , 
    ImageSize -> 900, Filling -> Axis, 
    PlotTheme -> "Web"]

Gives

LabelingFunction Result

$\endgroup$
2
  • $\begingroup$ Check "LabelingFunction" under "Options" in the ListPlot documentation. $\endgroup$
    – bbgodfrey
    Commented Jan 18, 2022 at 14:31
  • 1
    $\begingroup$ Crossposted here. $\endgroup$ Commented Jan 18, 2022 at 15:49

3 Answers 3

4
$\begingroup$

You need to write"LabelingFunction -> (LabelString[[#[1]]] &)":

LabelString = Table[StringForm["Test # ``", LS], {LS, 1, 25}];
ListPlot[Prime[Range[10]], 
 LabelingFunction -> (LabelString[[#[[1]]]] &), Filling -> Axis]

enter image description here

$\endgroup$
2
  • $\begingroup$ How do I use this if I were to use DiscretePlot instead of Listplot? $\endgroup$
    – L_R_G_2021
    Commented Jan 19, 2022 at 10:53
  • $\begingroup$ Try e.g.: dat = Prime[Range[10]]; DiscretePlot[dat[[n]], {n, 10}, LabelingFunction -> (LabelString[[#[[1]]]] &), ImageSize -> 900, Filling -> Axis, PlotTheme -> "Web"] $\endgroup$ Commented Jan 19, 2022 at 14:03
4
$\begingroup$

Or, simply

ListPlot[Prime[Range[10]] -> LabelString[[;; 10]], ImageSize -> 900, 
    Filling -> Axis, PlotTheme -> "Web"]

enter image description here

The length of the label list must equal the length of the data list, hence LabelString[[;; 10]]. The option, LabelingFunction, can be used to specify the location of the labels relative to the data points, if desired.

$\endgroup$
3
  • $\begingroup$ How do I use this if I were to use DiscretePlot instead of Listplot? $\endgroup$
    – L_R_G_2021
    Commented Jan 19, 2022 at 10:52
  • $\begingroup$ @L_R_G_2021 The method illustrated in my answer does not carry over directly to DiscretePlot. However, a simple alternative is DiscretePlot[Labeled[Prime[n], LabelString[[n]]], {n, 10}, ImageSize -> 900, Filling -> Axis, PlotTheme -> "Web"]. A third argument can be added to Labeled to specify the location of the data label. $\endgroup$
    – bbgodfrey
    Commented Jan 20, 2022 at 0:23
  • $\begingroup$ @L_R_G_2021 Labeled can be replaced by Callout in my previous comment with almost identical results. This latter approach is very similar to that of @BobHanson. $\endgroup$
    – bbgodfrey
    Commented Jan 20, 2022 at 0:59
3
$\begingroup$

Alternatively, you could use Callout

ListPlot[
 Callout[Prime[#],
    StringForm["Test # ``", #],
    Above] & /@ Range[10],
 ImageSize -> 900,
 Filling -> Axis,
 PlotTheme -> "Web"]

enter image description here

$\endgroup$
2
  • $\begingroup$ How do I use this if I were to use DiscretePlot instead of Listplot? $\endgroup$
    – L_R_G_2021
    Commented Jan 19, 2022 at 10:55
  • $\begingroup$ DiscretePlot[Callout[Prime[n], StringForm["Test # ``", n], Above], {n, 1, 10}, ImageSize -> 900, PlotTheme -> "Web"] $\endgroup$
    – Bob Hanlon
    Commented Jan 19, 2022 at 15:37

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.