5

Please consider this simple code:

Manipulate[
 Plot[{a Sin[x], a Cos[x]}, {x, 0, 2 Pi}, 
  PlotLegends -> "Expressions"], {a, 1, 2}]

Mathematica returns this output:

enter image description here

I believe this is a bug. Is there any workaround to cope with this problem?

I use Mathematica version 10.3.1 on Win 10 64bit. Thank you.

Karsten7
  • 27,448
  • 5
  • 73
  • 134
Sukan
  • 455
  • 3
  • 7

2 Answers2

4

The FrontEnd has the habit of renaming variables, which is usually a good thing, but sometimes can be troublesome. One possibility is to evaluate the argument of Plot, e.g.:

Manipulate[
   NumberForm[
       Plot @@ {{a Sin[x], a Cos[x]}, 
                {x, 0, 2 Pi},
                 PlotLegends -> "Expressions", 
                 PlotRange -> {-2, 2}}, {4, 3}
    ], 
    {a, 1., 2, Appearance -> "Labeled"}]

enter image description here

Rolf Mertig
  • 17,172
  • 1
  • 45
  • 76
  • The usage of Appearance is first time to me.I'm interested that how do you know it?? – yode Jan 20 '16 at 19:22
1

Legends are heavily affecting the performance in Dynamic so I'd probably go with some hand made legends anyway:

Manipulate[
 Grid[{{
    Dynamic @ Plot[{a Sin[x], a Cos[x]}, {x, 0, 2 Pi}]
    ,
    LineLegend[
     {RGBColor[0.368417, 0.506779, 0.709798], RGBColor[0.880722, 0.611041, 0.142051]},
     TraditionalForm /@ {Dynamic[a Defer@Sin[x]], Dynamic[a Defer@Cos[x]]}
     ]
    }}], {a, 1, 2}]

enter image description here

Colors for lines taken from Match colors to plot themes

Kuba
  • 136,707
  • 13
  • 279
  • 740
  • 2
    Why not simply Manipulate[ Plot[{a Sin[x], a Cos[x]}, {x, 0, 2 Pi}, PlotLegends -> {Dynamic[a Sin[x]], Dynamic[a Cos[x]]}], {a, 1, 2}]. I got same result. – Basheer Algohi Jan 21 '16 at 01:34
  • @Algohi I don't know how fast your machine is but on mine there is a noticable difference. The face that the Plot has to use PlotLegends slows it down a lot. – Kuba Jan 21 '16 at 07:19