2
$\begingroup$

(I am using MMA v.12.0.0 OSX.)

It seems like PlotRange is preventing PlotLabels from displaying the labels of the curve which have the same last data value.

I actually got this problem with DateListLogPlot but it seems to happen for different kind of plots.

Here is simple example with ListLinePlot :

data = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 5}, {11, 12, 13, 14, 5}, {4, 5, 
6, 7, 8}};

ListLinePlot[data, PlotLabels -> {"1", "2", "3", "4"}, Frame -> True]

enter image description here

but

ListLinePlot[data, PlotRange -> {{0, 5.5}, All}, 
PlotLabels -> {"1", "2", "3", "4"}, Frame -> True]

enter image description here

and I don't like this workaround with PlotRangePadding (the labels are at the right position but the lines (leaders) are too short as they stop at the frame) :

ListLinePlot[data, PlotRangePadding -> {{0, 1}, Automatic}, 
PlotLabels -> {"1", "2", "3", "4"}, Frame -> True]

enter image description here

$\endgroup$
  • $\begingroup$ Place the labels: ListLinePlot[data, PlotRange -> {{0, 5.5}, All}, PlotLabels -> Placed[Range[4], Scaled[0.5]], Frame -> True] $\endgroup$ – Bob Hanlon Mar 28 at 17:34
  • $\begingroup$ @BobHanlon Thanks, I've tried that but I want the labels to be in a column out of the frame (because I have many curves and the plot is crowded). I just want what would be expected in the second plot. $\endgroup$ – SquareOne Mar 28 at 17:52
  • $\begingroup$ @BobHanlon Sorry, i was not clear in the post concerning the position of the labels, i corrected it ("the labels are at the right position but the lines (leaders) are too short as they stop at the frame") $\endgroup$ – SquareOne Mar 28 at 17:59
2
$\begingroup$
data = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 5}, {11, 12, 13, 14, 5}, {4, 5, 6, 7, 
    8}};

The problem with the labels is caused by the final data values being the same. As a workaround, even a negligible offset (10^-6 in this case) eliminates the problem.

data2 = ReplacePart[data, {
    2 -> data[[2]] + 10.^-6,
    3 -> data[[3]] - 10.^-6}];

ListLinePlot[data2, PlotRange -> {{0, 5.5}, All}, PlotLabels -> Range[4], 
 Frame -> True]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ ); yes I thought about it but in my real life case I have many curves, regularly updated, I use tooltip to show the data values, and I display some of them ... so it would add some extra work. I would prefer not to modify the data but it seems that it will be the only workaround ... $\endgroup$ – SquareOne Mar 29 at 9:46
1
$\begingroup$

Generalizing Bob Hanlon's approach and coloring labels and callout curves to match the color of corresponding lines:

ClearAll[jitterEndPoint, colorCallouts]

jitterEndPoint[epsilon_: 10^-5] := MapAt[# + RandomReal[{-epsilon, epsilon}] &, #, 
  {All, -1}] &;

colorCallouts = # /. GraphicsGroup[a_] :> GraphicsGroup[Replace[a, b_BSplineCurve :>
    Style[b, Thin, Cases[a, Rotate[Style[_, col_, ___], ___] :> col, All]],  All]] &;

Examples:

data = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 5}, {11, 12, 13, 14, 5}, {4, 5, 6, 7, 8}};

Row[{#, colorCallouts@#2}] & @@ (ListLinePlot[# @ data, 
     PlotLabels -> (Style[#, ColorData[97]@#, 12] & /@ Range[Length@data]),
     ImageSize -> 500, Frame -> True, 
     PlotRange -> {{0, 5.5}, All}] & /@ {Identity, jitterEndPoint[]})

enter image description here

Row[{#, colorCallouts@#2}] & @@ (ListLinePlot[# @ data, 
     PlotLabels -> (Style[#, ColorData[97]@#, 12] & /@ Range[Length@data]),
     ImageSize -> 500, Frame -> True, 
     PlotRange -> {{0, 10.5}, All}] & /@ {Identity, jitterEndPoint[]})

enter image description here

SeedRandom[1]
dt = RandomInteger[100, {10, 5}];
dt[[;; 7, -1]] = 70;
dt[[8 ;;, -1]] = 30;

Row[{#, colorCallouts@#2}] & @@ (ListLinePlot[# @ dt, 
     PlotLabels -> (Style[#, ColorData[97]@#, 12] & /@ Range[Length@dt]),
     ImageSize -> 500, Frame -> True, 
     PlotRange -> {{0, 5.5}, All}] & /@ {Identity, jitterEndPoint[]})

enter image description here

With default setting for PlotRange we do not need jittering but coloring labels and/or callouts still helps:

Row[{#, colorCallouts@#}, Spacer[10]] & @
 ListLinePlot[data,  ImageSize -> 500, Frame -> True,
    PlotLabels -> (Style[#, ColorData[97]@#, 12] & /@ Range[Length@data])]

enter image description here

Row[{#, colorCallouts@#}, Spacer[10]] & @
 ListLinePlot[dt,  ImageSize -> 500, Frame -> True,
    PlotLabels -> (Style[#, ColorData[97]@#, 12] & /@ Range[Length@dt])]

enter image description here

| improve this answer | |
$\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.