# Plot legend for filling

I have a plot where several lines and curves intersect, and the regions between them are filled with red and blue. My code is

f[x_] = 2/(1 - 2 x^2); g[x_] = -(2/(1 + 2 x^2));
p1 = Plot[{g[x], -2}, {x, -2, -(1/Sqrt[2])},
PlotStyle -> ColorData[97, "ColorList"][[1]],
Filling -> {1 -> {2}}, FillingStyle -> {Blue, Opacity[0.5]}];
pa = Plot[{g[x], -2}, {x, -2, -(1/Sqrt[2])},
PlotStyle -> {Black, Black}];
p2 = Plot[{g[x], -2}, {x, -(1/Sqrt[2]), 1/Sqrt[2]},
Filling -> {1 -> {2}}, FillingStyle -> {Blue, Opacity[0.5]},
PlotStyle -> ColorData[97, "ColorList"][[1]]];
pb = Plot[{g[x], -2}, {x, -(1/Sqrt[2]), 1/Sqrt[2]},
PlotStyle -> {Black, Black}];
p3 = Plot[{g[x], -2}, {x, 1/Sqrt[2], 2}, Filling -> {1 -> {2}},
FillingStyle -> {Blue, Opacity[0.5]},
PlotStyle -> ColorData[97, "ColorList"][[1]]];
pc = Plot[{g[x], -2}, {x, 1/Sqrt[2], 2},
PlotStyle -> {Black, Black}];
p4 = Plot[{2, f[x]}, {x, -2, -(1/Sqrt[2])},
Filling -> {1 -> {{2}, LightRed}},
PlotStyle -> ColorData[97, "ColorList"][[1]]];
pd = Plot[{2, f[x]}, {x, -2, -(1/Sqrt[2])},
PlotStyle -> {Black, Black}];
p5 = Plot[{f[x], 2}, {x, -(1/Sqrt[2]), 1/Sqrt[2]},
Filling -> {1 -> {{2}, LightRed}},
PlotStyle -> ColorData[97, "ColorList"][[1]]];
pe = Plot[{f[x], 2}, {x, -(1/Sqrt[2]), 1/Sqrt[2]},
PlotStyle -> {Black, Black}];
p6 = Plot[{2, f[x]}, {x, 1/Sqrt[2], 2},
Filling -> {1 -> {{2}, LightRed}},
PlotStyle -> ColorData[97, "ColorList"][[1]]];
pf = Plot[{2, f[x]}, {x, 1/Sqrt[2], 2}, PlotStyle -> {Black, Black}];
line1 = Line[{{-(1/Sqrt[2]), -6}, {-(1/Sqrt[2]), 6}}];
line2 = Line[{{1/Sqrt[2], -6}, {1/Sqrt[2], 6}}];


It's horrible to look at, I know. It returns a nice plot though. What I would like to add to it are plot legends, as the red and blue regions indicate specific things. Can anyone help me?

Plot[{g[x], f[x], -2, 2}, {x, -2, 2}, PlotStyle -> Black,
Filling -> {1 -> {{3}, Opacity[0.5, Lighter @ Blue]},
2 -> {{4}, Opacity[.5, Lighter @ Red]}},
Exclusions -> None,
Frame -> True, Axes -> False, ImageSize -> Large,
PlotLegends -> Placed[SwatchLegend[{Lighter @ Blue, Lighter @ Red},
Style[#, 16]& /@ {"legend 1", "legend 2"}], {.8, .8}]]


Note: We can also set the filling option as:

Filling -> {1 -> {-2, Opacity[0.5, Lighter @ Blue]},
2 -> {2, Opacity[.5, Lighter @ Red]}}


We can set the position of the legend interactively using Locator:

DynamicModule[{pt = {.8, .8}},
Plot[{g[x], f[x], -2, 2}, {x, -2, 2},
PlotStyle -> Black,
Filling -> {1 -> {{3}, Opacity[0.5, Lighter @ Blue]},
2 -> {{4}, Opacity[.5, Lighter @ Red]}}, Exclusions -> None,
Frame -> True, Axes -> False, ImageSize -> Large,
Epilog -> Dynamic @
{Locator[Dynamic[pt], SwatchLegend[{Lighter @ Blue, Lighter @ Red},
Style[#, 16] & /@ {"legend 1", "legend 2"}]]}]]


• I like the Exclusions->None. Couldn't remember what the option to draw the two extra lines was called. Commented Nov 27, 2020 at 19:33
• Thank you so much! Commented Nov 29, 2020 at 15:24

This can be merged into a single Plot call which will make adding legend with Epilog easier. Here's an example

f[x_] = 2/(1 - 2 x^2);
g[x_] = -(2/(1 + 2 x^2));
Plot[
{
g[x], f[x],
-2, 2
}, {x, -2, 2},
PlotStyle -> Black,
Filling -> {
{
1 -> {{3}, Append[LightBlue, .5]},
2 -> {{4}, Append[LightRed, .5]}
}
},
Epilog -> {
Line[
{
{-(1/Sqrt[2]),
f[-(1/Sqrt[2]) - $MachineEpsilon]}, {-(1/Sqrt[2]), f[-(1/Sqrt[2]) +$MachineEpsilon]}
}
],
Line[
{
{(1/Sqrt[2]),
f[(1/Sqrt[2]) - $MachineEpsilon]}, {(1/Sqrt[2]), f[(1/Sqrt[2]) +$MachineEpsilon]}
}
],
Inset[
Style["This is a thing", Red],
{0, -4}
]
},
PlotRange -> {{-2, 2}, {-5, 5}},
PlotLegends -> {
"Function with a cusp",
"Something like a cosecant"
},
Frame -> True,
Axes -> False,
ImageSize -> 600
] // bringLegendIn[#, Scaled[{1, 1}], {Right, Top}] &


where


bringLegendIn[plot_, where_ : Scaled[{.05, .95}], anchor_ : {Left, Top}] :=

Show[#[[1]],
Epilog ->
Join[
Epilog /. Options[#[[1]], Epilog],
{
Inset[#[[2, 1]], where, anchor]
}
]
] &@plot;

• Thank you very, very much! Commented Nov 29, 2020 at 15:27