# Problems with RegionPlot and Legended

I have a RegionPlot that shows a couple of problems. The first one is that the region that borders the axes is overpassing them, there's a bit of the colour of the region outside the limit of the axes. I would like to know if that's something fixable.

Secondly, in that same plot the same happens to the framed legend, where its background passes through the lines of the frame.

There's the code. I don't know if that's enough to know what happens, if needed, ask for more.

xxx=1.01851;
yyy=0.375881;
sx=0.00421787;
sy=0.00178086;
Covxy=0.000905803;
l = Sqrt[sx^2 + sy^2];
d = MultinormalDistribution[{xxx,
yyy}, {{sx*100, Covxy*100}, {Covxy*100, sy*100}}];
sigma1 = 1/(l*100*Sqrt[2*Pi*E]);
sigma2 = 1/(l*100*Sqrt[2*Pi*E^4]);
sigma3 = 1/(l*100*Sqrt[2*Pi*E^9]);

RegionPlot[{PDF[d, {x, y}] > sigma1, PDF[d, {x, y}] < sigma1,
PDF[d, {x, y}] < sigma2, PDF[d, {x, y}] < sigma3}, {x, -2,
3}, {y, -2, 3},
PlotStyle -> {RGBColor[1, 0, 1], RGBColor[1, 0.6, 1],
RGBColor[1, 0.8, 1.], RGBColor[1, .9, 1]},
PlotRange -> {{-.3, 2.3}, {-1, 1.5}}, BoundaryStyle -> None,
Epilog -> {{Green, Point[{xxx, yyy}]}, {Red, Point[{0, 0}]}},
PlotLegends ->
Placed[Framed[
SwatchLegend[{RGBColor[1, 0, 1.], RGBColor[1, 0.6, 1],
RGBColor[1, 0.8, 1.], RGBColor[1, .9, 1]}, {"1\[Sigma]",
"2\[Sigma]", "3\[Sigma]", "4\[Sigma]"}],
Background -> White], {.955, .075}]]


It shows this:

Sorry for my English, it's not my first language. Also it's my first post so if there's something to be fixed, please tell me.

• Welcome to the Mathematica Stack Exchange. Please copy your own example from this post to a notebook with a fresh kernel and see what values you still need to provide for this to run. Thanks.
– Syed
Commented Nov 17, 2023 at 1:18
• Can you include the definitions of d, sigma1, ... so we can run your code? Commented Nov 17, 2023 at 1:19
• I don't see "the region that borders the axes is overpassing them, there's a bit of the colour of the region outside the limit of the axes" in the picture posted. Can you mark the problem areas by hand and post a picture?
– kglr
Commented Nov 17, 2023 at 9:33
• Please add the output of \$Version to your post. Thanks.
– Syed
Commented Nov 17, 2023 at 10:07
• @kglr It's actually pretty small, and now seeing the picture in this website does not seems to be that big as when I was seeing it in my Photos app. But I see that the legend looks kind of akward though. Commented Nov 18, 2023 at 10:26

Does this give the expected result?

I changed PlotRange to All and the placement of PlotLegends to {.9, ,2}

RegionPlot[{PDF[d, {x, y}] > sigma1, PDF[d, {x, y}] < sigma1,
PDF[d, {x, y}] < sigma2, PDF[d, {x, y}] < sigma3}, {x, -2,
3}, {y, -2, 3},
PlotStyle -> {RGBColor[1, 0, 1], RGBColor[1, 0.6, 1],
RGBColor[1, 0.8, 1.], RGBColor[1, .9, 1]}, PlotRange -> All,
BoundaryStyle -> None,
Epilog -> {{Green, Point[{xxx, yyy}]}, {Red, Point[{0, 0}]}},
PlotLegends ->
Placed[Framed[
SwatchLegend[{RGBColor[1, 0, 1.], RGBColor[1, 0.6, 1],
RGBColor[1, 0.8, 1.], RGBColor[1, .9, 1]}, {"1\[Sigma]",
"2\[Sigma]", "3\[Sigma]", "4\[Sigma]"}],
Background -> White], {.9, .2}]]


You can also try PlotRangePadding (consult documentation for its syntax)

RegionPlot[{PDF[d, {x, y}] > sigma1, PDF[d, {x, y}] < sigma1,
PDF[d, {x, y}] < sigma2, PDF[d, {x, y}] < sigma3}, {x, -2,
3}, {y, -2, 3},
PlotStyle -> {RGBColor[1, 0, 1], RGBColor[1, 0.6, 1],
RGBColor[1, 0.8, 1.], RGBColor[1, .9, 1]}, PlotRange -> All,
BoundaryStyle -> None,
Epilog -> {{Green, Point[{xxx, yyy}]}, {Red, Point[{0, 0}]}},
PlotLegends ->
Placed[Framed[
SwatchLegend[{RGBColor[1, 0, 1.], RGBColor[1, 0.6, 1],
RGBColor[1, 0.8, 1.], RGBColor[1, .9, 1]}, {"1\[Sigma]",
"2\[Sigma]", "3\[Sigma]", "4\[Sigma]"}],
Background -> White], {.9, .2}]]


• Not really, now this gives me a plot with the region near the edges being white, I mean, there's like a wide white line just beside the edges, its like the opposite of what was happening haha. Also, the legend stays the same. But thanks for trying. Commented Nov 17, 2023 at 9:17
• Oh I think that's better, it fixes the issue. Thank you so much. Commented Nov 18, 2023 at 10:34
• Thanks for the accept, I'm glad that I could help
– eldo
Commented Nov 18, 2023 at 10:35