4
$\begingroup$

I need to plot a list of points and I am using ListLinePlot. Mathematica decides which values on the y axis are labeled. How can I show labels for the exact y value of each point?

$\endgroup$
10
$\begingroup$

If you have only a couple of points you could try

data = {1.2, 4.3, 5.3, 2.721, 7.32, 1.7, 4.9};

ListLinePlot[
 data,
 Ticks -> {Automatic, data},
 GridLines -> {Automatic, data},
 PlotMarkers -> Automatic]

enter image description here

However, if points are not sufficiently apart ticks will be overwritten. Therefore I would recommend to use Tooltip.

ListLinePlot[
 Tooltip @ data,
 GridLines -> Automatic,
 PlotMarkers -> Automatic]

enter image description here

Setting the mouse pointer on any point you can see its exact value.

With not too many points you might consider

points =
  Text[#2, {##}] & @@@ Transpose[{Range@Length@data, data}] /.
   {a_, b_} :> {a - 0.3, b - 0.3};

ListLinePlot[Tooltip@data,
 Epilog -> points,
 GridLines -> Automatic,
 PlotMarkers -> Automatic]

enter image description here

An unsuccessful rebellion against the closing votes:

data =
  Round[Table[Re[Zeta[1/2 + I x]], {x, 20, 40, 1}], 0.01];

points =
  Text[Framed[#2, FrameMargins -> {{3, 3}, {0, 0}}, Background -> White],
       {##}] & @@@ Transpose[{Range[20, 40], data}] /.
         {a_, b_} :> {a, b - 0.12} /.                   
            Framed[a_, b__] :> Framed[PaddedForm[a, {2, 2}], b];

ListLinePlot[
 Tooltip@data,
 Epilog -> points,
 DataRange -> {20, 40},
 Filling -> Axis,
 FillingStyle -> Directive[Opacity[0.5], LightBlue],
 Frame -> True,
 GridLines -> Automatic,
 ImageSize -> 600,
 MeshStyle -> Directive[Blue, PointSize[Large]],
 PlotMarkers -> Automatic,
 PlotRangePadding -> {{0.9, 0.5}, {0.3, 0.1}},
 PlotStyle -> GrayLevel@0.7]

enter image description here

$\endgroup$
  • $\begingroup$ As an alternative to {a_, b_} :> {a, b - 0.12} you could just add an offset to Text. Try Text[..., {0, 1.75}]] $\endgroup$ – Mike Honeychurch Oct 2 '14 at 21:43
4
$\begingroup$

As pointed out by @eldo there is a risk of the labels overlapping. In addition to the methods he mentioned to deal with this, you can reduce the likelihood of overlap by using a Frame and alternating labels between left and right FrameTicks.

data = Round[#, .01] & /@ RandomReal[{0, 7}, {9, 2}] // Sort;

yValues = data[[All, 2]] // Sort;

{yLeft, yRight} = ({yValues, ""} //
       Flatten // Partition[#, 2] & //
       Transpose) /. "" -> Sequence[];
(* padded to handle cases with odd number of points *)

ListLinePlot[Tooltip /@ data,
 Frame -> True, Axes -> False,
 FrameTicks -> {{yLeft, yRight}, {Automatic, None}},
 GridLines -> {Automatic, yValues},
 PlotMarkers -> Automatic]

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.