4
$\begingroup$

I'm creating a fairly simple plot with a bunch of similar graphs in it. It looks something like this (but with more than two functions):

Plot[{x + 2, x - 2}, {x, -3, 3}, PlotRange -> {{-3.1, 3.1}, {-4, 4}},
    PlotLegends -> "Expressions"]

And it produces the following plot, as expected:

enter image description here

But I want each equation in the legend to say "$y=\ldots$", i.e. I want to produce the following:

Plot[{x + 2, x - 2}, {x, -3, 3}, PlotRange -> {{-3.1, 3.1}, {-4, 4}},
    PlotLegends -> {"y=x+2", "y=x-2"}]

enter image description here

As you can see, I manually typed all equations into PlotLegends. But I don't want to do that, because in my actual application I have more than two functions on the same graph. Is it possible to still use "Expressions" in PlotLegends, but to somehow modify all of them at once, because I want to add "y=" to all of them?

$\endgroup$

2 Answers 2

5
$\begingroup$

You could put y = in the functions

Plot[{y = x + 2, y = x - 2}, {x, -3, 3}, 
 PlotRange -> {{-3.1, 3.1}, {-4, 4}}, PlotLegends -> "Expressions"]
$\endgroup$
3
$\begingroup$

One way is:

f = {x + 2, x - 2};
Plot[f, {x, -3, 3}, PlotRange -> {{-3.1, 3.1}, {-4, 4}},  PlotLegends -> {Row[{"y", #}, "="] & /@ f}]
$\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.