5
$\begingroup$

For some odd reason the PlotLegends output is not as expected.

Clear[f, g, v]
v = Function[x, k*x^-k];
g = Function[x, (x^-k)*Sin[x^k]/(1 + x^k)];
f = Function[x, x^k/(1 + x^k)];
With[{f = f, g = g}, 
Manipulate[Plot[{v[x], f[x], g[x], D[f[x] - g[x]]}, {x, 0, 5},
PlotLegends -> "Expressions"], {k, 1, 10}]]

enter image description here

$\endgroup$
3
  • 3
    $\begingroup$ What specifically is wrong with the output? $\endgroup$
    – whuber
    Commented May 20, 2013 at 19:38
  • $\begingroup$ I have to clarify that there is nothing wrong with the geometry of the function. The problem is the legend. The expression is rendered all wrong, $\endgroup$ Commented May 21, 2013 at 4:17
  • 1
    $\begingroup$ @JoseECalderon: There is an answer that most probably solves your problem already, but I think your question would be much better if you state what it is that you expect (or more precisely want) to get from that input. I understand that the output is not what you want, but it seems to be what can be expected with some knowledge about the evaluation order and scoping quirks of Mathematica (where the latter is IMHO not exactly its strongest part)... $\endgroup$ Commented May 21, 2013 at 10:10

1 Answer 1

13
$\begingroup$

The expression that is plotted in the legend are the provided pure functions (with some scoped internal variable names). PlotLegends is somewhat confused so we have to provide explicit entries for the Legend. Since the desired result is somewhat unclear here a few suggestions:

v[x_, k_] := k*x^-k;
g[x_, k_] := (x^-k)*Sin[x^k]/(1 + x^k);
f[x_, k_] := x^k/(1 + x^k);
Manipulate[Plot[{v[x, k], f[x, k], g[x, k], D[f[x, k] - g[x, k]]}, {x, 0, 5},
                PlotLegends -> "Expressions"], {k, 1, 10}]

enter image description here

or, with the explicit expressions:

Manipulate[Plot[{v[x, k], f[x, k], g[x, k], D[f[x, k] - g[x, k]]}, {x, 0, 5},
            PlotLegends -> TraditionalForm /@ {v[x, k], f[x, k], g[x, k], 
                           D[f[x, k] - g[x, k]]}],
           {k, 1, 10}]

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.