5

Consider this example:

Plot[Table[BesselJ[n, x], {n, 2}], {x, 0, 10}, 
 PlotLegends -> {"n=1", "n=2"}, Evaluated -> True]

The results in MMA v9 & V10 are:

V9:

enter image description here

V10:

enter image description here

Any idea why V10 can't pick up all PlotLegends? Thanks.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78

2 Answers2

3

I am in the middle of the long process of Tracing this issue. I don't yet have a patch but I can point to a part of the code that affects this. The function is:

Legending`LegendDump`plotLegendParser

Within its long definition we have a piece:

Legending`LegendDump`length = Switch[Legending`LegendDump`tag,
  0, Legending`LegendDump`optDimensions,
  _Integer, Legending`LegendDump`optDimensions[[Legending`LegendDump`tag]],
  "Generic", 
  If[Legending`LegendDump`arglength === Null, Switch[Legending`LegendDump`argexp,
    Null, 0,
    _List, Length[Legending`LegendDump`argexp],
    _, 1], Legending`LegendDump`arglength]]

This is where the number of functions is determined for the sake of legend generation. The critical part here is:

Length[Legending`LegendDump`argexp]

Which in the case of the example in the question expands to:

Length[{HoldForm[Table[BesselJ[n, x], {n, 2}]]}]

The plot function is being passed unevaluated (held by HoldForm) to Legending`LegendDump`plotLegendParser, which is exactly the sort of problem I expected. I have confirmed that replacing Length[. . .] in this definition with 2 causes the legend to be draw properly for this example, but I have not yet figured out how to determine that value automatically.

This answer is a work in progress but I wanted to get this down before I forget it. It may also serve as a starting point for anyone else who wishes to tackle the problem.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
2

Not really an idea or insight, but a workaround (on 10.0 for Mac OS X x86 (64-bit) (September 10, 2014);

Plot[Table[BesselJ[n, x], {n, 2}], {x, 0, 10},  
 PlotStyle -> {Red, Blue},  
 PlotLegends -> LineLegend[{Red, Blue}, {"n=1", "n=2"}],  
 Evaluated -> True]

enter image description here

The "Help" is not really a help:

enter image description here enter image description here

except the workaround I can not offer an explanation for this behavior.