2
$\begingroup$

First post. Hope it's not ignorant. I am trying to label several data points so I can label them properly on a ListLinePlot. I know I can label plots if I am generating data, but I am dealing with experimental data.

The end goal is to be able to graph the points, and have everything labeled properly. I am trying to figure out how not to do this the "dumb" way.

Here's what I want to label:

litValues = 
  {"Literature Values (kcal/mol)", 
   123.34, 214.97, 191.35, 259.66, 335.15, 314.03, 401.75, 497.27};
plot = 
  ListLinePlot[{Delete[litValues, 1]}, 
    AxesLabel -> {"Atomic number", "Ionization Energy (kj/mol)"}]

If my tone is wonky, excuse me -- I am very tired.

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I must admit that I don't quite understand what the question is, mostly I think because I do not know what you mean by "properly." You have already managed to have a label on the x axis and on the y axis. How do you want to change it? $\endgroup$ – C. E. Feb 8 '14 at 2:27
  • $\begingroup$ @Pickett I took it they wanted to put label on the points, with the value of each point. May be I also mis-understood it. $\endgroup$ – Nasser Feb 8 '14 at 2:40
  • $\begingroup$ @Nasser, you've got it. I would like to be able to label all of my points as they appear on the graph. Apologies for ambiguity. $\endgroup$ – Qu0rk Feb 9 '14 at 2:50
  • $\begingroup$ related Q/A: Label markers in ListPlot $\endgroup$ – kglr Oct 19 '14 at 22:03
4
$\begingroup$

if you want to label the points, one way is to build the labels yourself. (I thought there is an easier way to do this, but do not remember now). But here is an attempt

Mathematica graphics

litValues = {"Literature Values (kcal/mol)", 123.34, 214.97, 191.35, 259.66, 
    335.15, 314.03, 401.75, 497.27};
data = Rest@litValues;
lbls = MapIndexed[Text[Style[#1, 11], {First@#2, #1}, {0, 1}] &, data];

plot = 
  ListLinePlot[data, 
    AxesLabel -> {"Atomic number", "Ionization Energy (kj/mol)"}, 
    Mesh -> All, 
    ImageSize -> 500, 
    PlotStyle -> {Red, PointSize[0.02]}, 
    PlotRange -> {{0, Length@data + 1}, {0.6 Min@data, 1.3 Max@data}}, 
    Epilog -> lbls]
$\endgroup$
  • $\begingroup$ How about lbls = MapThread[Text[Style[#1, 11], {#2, #1}, {0, 1}] &, {data, Range@Length@data}]? I find this a little easier to understand than MapIndexed, but perhaps that's just me. $\endgroup$ – m_goldberg Feb 8 '14 at 5:11
  • $\begingroup$ @m_goldberg yes, that is a nice way to do it as well. As typical in Mathematica, there is always at least 10 ways to do the same thing :) $\endgroup$ – Nasser Feb 8 '14 at 5:18
  • $\begingroup$ Thanks, this solution offers flexibility for future reference. $\endgroup$ – Qu0rk Feb 10 '14 at 21:20
3
$\begingroup$

Another self made approach (using the presented data and desied labels):

ListPlot[txt = Rest@litValues, Joined -> True, 
 Epilog -> (Text[#[[2]], # + {0, 20}] & /@ 
    Transpose[{Range[Length@txt], txt}]), 
 PlotMarkers -> { Automatic, 8}, 
 BaseStyle -> {FontFamily -> "Kartika", 16}, 
 PlotRange -> {{0, 9}, {0, 600}}, Frame -> True, 
 FrameTicks -> {Range[8], Automatic, None, None}, 
 FrameLabel -> {"Atomic Number", "Ionization Energy"},ImageSize->600]

enter image description here

$\endgroup$
1
$\begingroup$

Update: Callout in versions 11.0+:

ListLinePlot[Callout[#, #]& /@ (Rest @ litValues), 
 AxesLabel -> {"Atomic number", "Ionization Energy (kj/mol)"},
 ImageSize -> 500, PlotStyle -> Red, PlotMarkers -> {Automatic, 14}]

enter image description here

Original answer:

You can also pre-process the data to style and label it:

labeleddata = Labeled[#, Style[#, 11], Below] & /@ Rest[litValues];

ListLinePlot[labeleddata,
 AxesLabel -> {"Atomic number", "Ionization Energy (kj/mol)"},
 ImageSize -> 500, PlotStyle -> Red, PlotMarkers -> {Automatic, 14},
 PlotRange -> {{0, Length@labeleddata + 1}, {0.6 Min@Rest[litValues], Automatic}}]

enter image description here

Or, put the labels After the plotmarkers using

labeleddata = Labeled[#, Style[#, 11], After] & /@ Rest[litValues];

enter image description here

When used without a position specification, Labeled positions the labels in a way to avoid collisions (see this answer by @Mr.Wizard to a closely related question):

labeleddata = Labeled[#, Style[#, 11]] & /@ Rest[litValues];

enter image description here

$\endgroup$

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.