0

I'm having trouble getting all of my lines within a Plot[] rendering to be presented in the legend. Below I illustrate the problem. I create 4 different lines in Plot[] but the legend only has the first entry.

(*set up functions*)
piFunction = (1 - Exp[-Δx a])/(1 - 
    Exp[-Δx 2 Ne]);(*StatMech Fixation Pr*)
rate = 
 mu 2/a Ne piFunction;(*Subscript[q,ij]:rate of transitioning from i \
to j*)
logRate = Log[10, rate] // Simplify;
logRateApproxList = 
  Table[Series[logRate, {Δx, 0, order}] // 
     Simplify, {order, 1, 20}] // Normal;
logRateApproxListLargeNe = 
  logRateApproxList /. {a -> 0, 
     mu -> 1, Δx -> Δy/Ne} // Cancel;
(*Plot subset of Functions*)

subList = {3, 7, 11, 15};
Plot[logRateApproxListLargeNe[[subList]], {Δy, -4, 4}, 
 AxesLabel -> {"Δx Ne", "Θ"}, 
 Evaluated -> True, PlotStyle -> Automatic, 
 PlotLegends -> 
  LineLegend[subList, LegendMarkers -> Automatic, 
   LegendFunction -> (Framed[#, RoundingRadius -> 5] &), 
   LegendLabel -> 
    Column[{"Δx", "Approximation Order"}]]]

And here's the output,

Plot with 4 function evaluations but only one entry in the legend

Please note I would like to utilize the PlotStyle->Automatic functionality rather than having to define the PlotStyle explicitly.

I'm using Mathematica 10.4.1 on Ubuntu 14.04

Feyre
  • 8,597
  • 2
  • 27
  • 46
mikemtnbikes
  • 679
  • 3
  • 11
  • This is a known problem (and duplicate); Evaluated -> True doesn't work well with legends. use Evaluate @ logRateApproxListLargeNe[[subList]] instead, but be mindful that does not localize the Plot variables. – Mr.Wizard Aug 09 '16 at 12:58
  • I think it should also be pointed out that Evaluated is undocumented, so while it works in most cases, it can't be trusted to work in all cases. – rcollyer Aug 09 '16 at 13:01
  • @rcollyer How is it that needful things like Evaluated are relegated to "undocumented" and "can't be trusted" while the function bloat continues? Gah... – Mr.Wizard Aug 09 '16 at 13:03
  • @Mr.Wizard it lacks documentation, and like all such functions ... as to the function bloat, I'm no where near the strategy meetings so I can't comment. – rcollyer Aug 09 '16 at 13:05
  • 1
    @rcollyer Sorry, just venting nebulous disappointment. :-( – Mr.Wizard Aug 09 '16 at 13:06
  • 1
    @Mr.Wizard duly noted. There are always things we (on the inside) wish were included, too. But, schedules get away from us, and perfection is the enemy of the good. – rcollyer Aug 09 '16 at 13:10

1 Answers1

4

It is often better to wrap the functions in Evaluate[] rather than set Evaluated->True:

Plot[Evaluate[
  logRateApproxListLargeNe[[subList]]], {Δy, -4, 4}, 
 AxesLabel -> {"Δx Ne", "Θ"}, 
 PlotStyle -> Automatic, 
 PlotLegends -> 
  LineLegend[subList, LegendMarkers -> Automatic, 
   LegendFunction -> (Framed[#, RoundingRadius -> 5] &), 
   LegendLabel -> 
    Column[{"Δx", "Approximation Order"}]]]

enter image description here

  • You may wish to wrap the Plot expression in Block[{Δy}, (* Plot *)] if there is any chance that Δy may have a global value.
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Feyre
  • 8,597
  • 2
  • 27
  • 46
  • @Mr.Wizard Well, to be honest it might as well just have been a comment. But I tend to overlook that when a Q gets no attention for 20 mins. I can delete if you think best. – Feyre Aug 09 '16 at 13:06
  • No, you put in the time to write the answer, there is no need to delete it. I merely wanted to point out that there is also reason to use Evaluated -> True, documented or not. – Mr.Wizard Aug 09 '16 at 13:10