5
$\begingroup$

I've asked this question in Wolfram community and have not received a response.

I am wondering why when I use Show to combine 6 plots, the plot legend does not show in order? The order should be 150, 250, 350, 450, 550, 650. However when I step through Show adding one plot at a time, I notice that Mathematica does something weird with the order of the legend, and in the end the order is 650, 150, 250, 350, 450, 550. How can I get the order of the Legend or Show to be correct, such that there is a logical sequence of the label? I have attached some code below to show what's happening:

 data = {
     {0., 23.1628, 15., 12., 5., 2., .75, 0.0},
     {0., 53.611, 100., 83.27448, 40., 33., 25., 10.0},
     {0., 71.7055, 100., 79, 72.996, 30.76314, 17., 15.},
     {0., 71.92428, 100., 100, 83.35182, 41.09816, 35., 10.0},
     {0., 75.87498, 100., 100., 88.65755, 87.0691, 34.27131, 20.},
     {0., 100., 100., 100., 88.65755, 87.0691, 80.4311, 39.40}
     } // TableForm;

time = {0., 120., 300., 600., 900., 1200., 1500., 1800.};

activity = Table[Transpose[{time, data[[1, i]]}], {i, 1, 6}];

tickSpecs = {{{0, "0"}, {120, "2"}, {300, "5"}, {600, "10"}, {900,
     "15"}, {1200, "20"}, {1500, "25"}, {1800, "30"}}, Automatic};

label = {"150 ", "250 ", "350 ", "450 ", "550 ", "650 "};
colors = {Black, Red, Orange, Green, Blue, Purple, Magenta};


Plots = Table[
     ListPlot[activity[[i]],

    PlotRange -> All,

    PlotStyle -> {Darker[colors[[i]]], Dashing[{0.05}], Thick,
      AbsolutePointSize[12]},
    Joined -> True,
    Mesh -> All,
    Ticks -> {Automatic, None},
    FrameTicks -> tickSpecs,
    Frame -> {Left, Bottom},
    FrameStyle -> Thick,
    Axes -> False,
    LabelStyle -> {FontSize -> 20, FormatType -> Bold},
    ImageSize -> 450,
    PlotLegends ->
     LineLegend[{label[[i]]},
      LabelStyle -> Directive[Bold, 15],
      LegendMarkerSize -> Automatic]],
{i, 1, 6}];

Show[Plots]
$\endgroup$
1
  • $\begingroup$ In version 10.0.2 under Windows your code produces a legend in the order 150, 250, 350, 450, 550, 650 as desired. Therefore I think this was a bug that has been fixed. $\endgroup$
    – Mr.Wizard
    Feb 19, 2015 at 16:11

5 Answers 5

4
$\begingroup$

Also, without the fuss of tables and Showand disregarding unnecessary options:

ListLinePlot[activity,
 FrameTicks -> tickSpecs, 
 Frame -> {Left, Bottom}, FrameStyle -> Thick, Axes -> False, 
 LabelStyle -> {FontSize -> 20, FormatType -> Bold}, 
 PlotStyle -> (Directive[Darker@#, Dashing[{0.05}], Thick] & /@ colors), 
 PlotLegends -> LineLegend[label, LabelStyle -> Directive[Bold, 15]], 
 PlotMarkers -> ({Graphics[{Darker@#, Disk[]}], .05} & /@ colors)]

Mathematica graphics

$\endgroup$
3
$\begingroup$

I've run into several issues like this with the legends and quite frankly, trying to investigate the cause/fix it is probably more trouble than worth. Instead, here's a work around:

(* Your code without the legends *)
Plots = Table[
   ListPlot[activity[[i]], PlotRange -> All, 
    PlotStyle -> {Darker[colors[[i]]], Dashing[{0.05}], Thick, 
      AbsolutePointSize[12]}, Joined -> True, Mesh -> All, 
    Ticks -> {Automatic, None}, FrameTicks -> tickSpecs, 
    Frame -> {Left, Bottom}, FrameStyle -> Thick, Axes -> False, 
    LabelStyle -> {FontSize -> 20, FormatType -> Bold}, 
    ImageSize -> 450], {i, 1, 6}];

(* Construct legends separately *)
Row[{
    Show[Plots],
    LineLegend[Directive@{Darker@#, Thick, Dashing[{0.05}]} & /@ Most@colors, label, 
        LabelStyle -> Directive[Bold, 15], LegendMarkerSize -> Automatic]
}]

I think this might be a bug, since this errant behaviour does not occur in Mathematica 10 on the Raspberry Pi.

$\endgroup$
1
  • $\begingroup$ Great! Thank you, The work around works great, and also helps me with more exposure to functional programming! $\endgroup$
    – tarhawk
    Mar 24, 2014 at 16:33
2
$\begingroup$

There may be cases where Show[] is preferred (e.g. when combining ListPlot and Plot). In that case nesting the Show function was found to preserve the order of the legend items.

In the above example, the following was found to preserve the legend order.

Show[Plots[[1]]];
Show[%, Plots[[2]]];
Show[%, Plots[[3]]];
Show[%, Plots[[4]]];
Show[%, Plots[[5]]];
Show[%, Plots[[6]]]

One may use the following function.

Show2[plots__] := Fold[Show,  Show[(List @ plots)[[1]]],  (List @ plots)[[2 ;; -1]]]

But since Plots is a list, the head 'List' should be removed with the @@ operator to obtain a sequence:

 Show2 @@ Plots
$\endgroup$
1
$\begingroup$

You can do a simple manual fix like so :-

Show[Extract[Plots, {{5}, {4}, {3}, {2}, {1}, {6}}]]

enter image description here

$\endgroup$
1
  • $\begingroup$ Okay great! Thank you! $\endgroup$
    – tarhawk
    Mar 24, 2014 at 16:32
1
$\begingroup$
ListLinePlot[Legended[#, Style[#2, Bold, 15]] & @@@ Transpose[{activity, label}],
 Axes -> False, FrameTicks -> tickSpecs, Frame -> {True, True, False, False},  
 FrameStyle -> Directive[FontSize -> 20, FontWeight -> Bold],
 PlotStyle -> (Thread[{Darker /@ colors, Dashing[{0.05}], Thick}]),
 PlotMarkers -> ({Graphics[{Dynamic@CurrentValue["Color"], Disk[]}], .05})]

enter image description here

$\endgroup$

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.