# Adding legends when using Show

I want to show my students that $f(x)=2\sin x-3$ and $f^{-1}(x)=\sin^{-1}\dfrac{x+3}{2}$ are inverses of one another via visual evidence; that is, $f$ and $f^{-1}$ are reflections of one another across the line $y=x$. Thus far, I have:

Show[
Plot[2 Sin[x] - 3, {x, -π/2, π/2},
AxesOrigin -> {0, 0}],
Plot[ArcSin[(x + 3)/2], {x, -5, -1}],
Plot[x, {x, -5, 5}],
PlotRange -> {{-5, 5}, {-5, 5}},
AxesLabel -> {"x", "y"},
AspectRatio -> Automatic
]


Which gives this image:

Note that I've restricted $f$ and $f^-1$ to their domains. I'm wondering how I can add plot legends for each curve in this situation.

I would add PlotLabels to each plot separately:

Show[Plot[2 Sin[x] - 3, {x, -π/2, π/2}, AxesOrigin -> {0, 0},
PlotLabels -> "sin"],
Plot[ArcSin[(x + 3)/2], {x, -5, -1}, PlotLabels -> "arcsin"],
Plot[x, {x, -5, 5}, PlotLabels -> "x"],
PlotRange -> {{-5, 5}, {-5, 5}}, AxesLabel -> {"x", "y"},
AspectRatio -> Automatic]


Or making the plot colorful first:

plot = Show[
Plot[2 Sin[x] - 3, {x, -π/2, π/2}, AxesOrigin -> {0, 0},
PlotStyle -> Red],
Plot[ArcSin[(x + 3)/2], {x, -5, -1}, PlotStyle -> Green],
Plot[x, {x, -5, 5}, PlotStyle -> Blue],
PlotRange -> {{-5, 5}, {-5, 5}}, AxesLabel -> {"x", "y"},
AspectRatio -> Automatic]


and then using ShowLegend:

Needs["PlotLegends"]
ShowLegend[plot, {{{Graphics[{Red, Line[{{0, 0}, {2, 0}}]}],
"sin"}, {Graphics[{Green, Line[{{0, 0}, {2, 0}}]}],
"arcsin"}, {Graphics[{Blue, Line[{{0, 0}, {2, 0}}]}], "x"}},
LegendPosition -> {0.7, 0.2}, LegendSize -> {0.45, 0.4},


Or, to incorporate rcollyer's comment below:

Legended[plot, LineLegend[{Red, Green, Blue}, {"sin", "arcsin", "x"}]]


• ShowLegends is deprecated as of v9. If you're using PlotLabels, then you have v11. So, I'd use Legended[plot, LineLegend[{Red, Green, Blue}, {"sin", "arcsin", "x"}]]. Commented Sep 26, 2016 at 19:56
• @rcollyer I think that qualifies as an answer. // I was thinking how to show a legend, so ShowLegend naturally came in mind. // Btw, I'm on v10.4 Commented Sep 26, 2016 at 20:00
• huh. The versions just sort of blur together after a while. Commented Sep 26, 2016 at 20:53

You can try this (like here):

funcList[x_] = {2 Sin[x] - 3, ArcSin[(x + 3)/2], x};
funcRange = {{-π/2, π/2}, {-5, -1}, {-5, 5}};

Plot[Evaluate@MapThread[ConditionalExpression[#1, First@#2 <= x <Last@#2] &
, {funcList[x],funcRange}], {x, -5, 5}, PlotLegends -> funcList[x]
, AxesLabel -> {"x", "y"}, AspectRatio -> Automatic]
`

• Welcome! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!
– user9660
Commented Sep 26, 2016 at 11:49