3
$\begingroup$

In Mathematica 13.2, while defining a function for manipulating and animating a plot of multiple functions, I'm unable to generate a legend for the second and subsequent functions in my list. I've narrowed down the problem to the use of ReplaceAll(/.).

Here is a simplified demo:

  1. The first Plot[] does not contain a /. and behaves correctly: Each function gets its own color and legend.
  2. The second Plot[] contains a /. and plots all functions with the same color and only the first function gets a legend.
  3. The third Plot[] adds an Evaluated -> True directive, which fixes the color but not the legend issue.

Plot[{x, x^2, x^3}, {x, -1, 1}, PlotLegends -> {"n=1", "n=2", "n=3"}]

Plot[{x, x^2, x^3} /. {}, {x, -1, 1}, PlotLegends -> {"n=1", "n=2", "n=3"}]

Plot[{x, x^2, x^3} /. {}, {x, -1, 1}, Evaluated -> True, PlotLegends -> {"n=1", "n=2", "n=3"}]

Mathematica plots of the functions x, x^2, and x^3, exhibiting incorrect coloration and legends

In my actual code, I make use of the ReplaceAll directive to expose parameters to Manipulate[] so I'd like to learn how to get legends to work with /..

I'm new to Mathematica and, from what I could tell after extensive searching on MMA.SE, the Evaluate -> True directive ought to have been enough to get Plot[] to treat its input as a true list and map each list item to a separate legend text. Clearly I'm missing some nuance. Any help would be appreciated!

$\endgroup$

1 Answer 1

5
$\begingroup$

Try this:

Plot[Evaluate[({x, x^2, x^3} /. {})], {x, -1, 1}, PlotLegends -> {"n=1", "n=2", "n=3"}] 

enter image description here

Have fun!

$\endgroup$
1
  • $\begingroup$ Brilliant! I was previously trying to wrap Evaluate[] around the function list itself but it makes sense why it should be wrapped around the ReplaceAll instead. Thank you! $\endgroup$ Commented Jan 21, 2023 at 10:44

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.