5
$\begingroup$

I use these commands

Show[ContourPlot[ PDF[MultinormalDistribution[{0, 0}, {{2, 1/2}, {1/2, 1}}], {x, y}],
 {x, -2, 2}, {y, -2, 2}, Contours -> {0.05, 0.09}, ContourShading -> {None, Green},    
ContourStyle -> {{Thickness[0.004], Opacity[1]}, {Thickness[0.004], Opacity[1]}}], 
ContourPlot[PDF[MultinormalDistribution[{0, 0}, {{2, 1/2}, {1/2, 1}}], {x, y}], 
{x, -2, 2}, {y, -2, 2}, Contours -> {0.06, 0.07}, ContourShading -> {None, Yellow}, 
ContourStyle -> {{Thickness[0.005], Dashed, 
Opacity[1]}, {Thickness[0.005], Dashed, Opacity[1]}}]]

to plot the figure on the left enter image description here

I want to "cut" the area corresponding to $\mathbb{R}^2-\{x\geq 0 \&\& y\geq 0\}$ to obtain something like the figure on the right, i.e. covering the cutted area with a texture made of diagonal lines.

How can I do? I tried with "Mesh" but the lines appear in all the Frame and not only in the interested areas.

$\endgroup$
4
$\begingroup$
dist = MultinormalDistribution[
   {0, 0}, {{2, 1/2}, {1/2, 1}}];

rgn = ImplicitRegion[{0.05 <= PDF[dist, {x, y}] <= 0.09,
    -2 <= x <= 2, -2 <= y <= 2, ! (x >= 0 && y >= 0)}, {x, y}];

Show[
 ContourPlot[
  PDF[dist, {x, y}], {x, -2, 2}, {y, -2, 2},
  Contours -> {0.05, 0.09},
  ContourShading -> {None, Green},
  ContourStyle -> Thickness[0.004]],
 ContourPlot[
  PDF[dist, {x, y}], {x, -2, 2}, {y, -2, 2},
  Contours -> {0.06, 0.07},
  ContourShading -> {None, Yellow},
  ContourStyle ->
   Directive[Thickness[0.005], Dashed]],
 ContourPlot[(x + y) Boole[! (x >= 0 && y >= 0)],
  {x, -2, 2}, {y, -2, 2},
  Contours -> 50,
  ContourShading -> None,
  RegionFunction -> ({#1, #2} ∈ rgn &)]]

enter image description here

EDIT: A variation using RegionPlot

{outerRgn, innerRgn, rgn} =
  ImplicitRegion[
     {#[[1]] <= PDF[dist, {x, y}] <= #[[2]],
      -2 <= x <= 2, -2 <= y <= 2, #[[3]]}, {x, y}] & /@
   {{0.05, 0.09, 
     True}, {0.06, 0.07, True}, {0.05, 0.09, ! (x >= 0 && y >= 0)}};

Show[
 RegionPlot[{outerRgn, innerRgn},
  PlotRange -> {{-2, 2}, {-2, 2}},
  PlotStyle -> {Green, Yellow}],
 ContourPlot[(x + y) Boole[! (x >= 0 && y >= 0)],
  {x, -2, 2}, {y, -2, 2},
  Contours -> 50,
  ContourShading -> None,
  RegionFunction -> ({#1, #2} \[Element] rgn &),
  BoundaryStyle -> Thick]]

enter image description here

$\endgroup$
1
$\begingroup$

Add a RegionFunction:

Show[
 ContourPlot[
  PDF[MultinormalDistribution[{0, 0}, {{2, 1/2}, {1/2, 1}}], {x, 
    y}], {x, -2, 2}, {y, -2, 2}, Contours -> {0.05, 0.09}, 
  ContourShading -> {None, Green}, 
  ContourStyle -> {{Thickness[0.004], Opacity[1]}, {Thickness[0.004], 
     Opacity[1]}}, 
  RegionFunction -> Function[{x, y, z}, x >= 0 && y >= 0]], 
 ContourPlot[
  PDF[MultinormalDistribution[{0, 0}, {{2, 1/2}, {1/2, 1}}], {x, 
    y}], {x, -2, 2}, {y, -2, 2}, Contours -> {0.06, 0.07}, 
  ContourShading -> {None, Yellow}, 
  ContourStyle -> {{Thickness[0.005], Dashed, 
     Opacity[1]}, {Thickness[0.005], Dashed, Opacity[1]}}, 
  RegionFunction -> Function[{x, y, z}, x >= 0 && y >= 0]]]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Thank you, but this is not exactly what I want. I would like to show the cutted are but with a texture made of diagonal lines, as shown in the figure on the right $\endgroup$ – apt45 Aug 31 '16 at 19:52
1
$\begingroup$

If I understand which region you are interested in correctly:

Show[ContourPlot[
  PDF[MultinormalDistribution[{0, 0}, {{2, 1/2}, {1/2, 1}}], {x, 
    y}], {x, -2, 2}, {y, -2, 2}, Contours -> {0.05, 0.09}, 
  ContourShading -> {None, Green}, 
  ContourStyle -> {{Thickness[0.004], Opacity[1]}, {Thickness[0.004], 
     Opacity[1]}}, 
  RegionFunction -> Function[{x, y, z}, x <= 0 || y <= 0]], 
 ContourPlot[
  PDF[MultinormalDistribution[{0, 0}, {{2, 1/2}, {1/2, 1}}], {x, 
    y}], {x, -2, 2}, {y, -2, 2}, Contours -> {0.06, 0.07}, 
  ContourShading -> {None, Yellow}, 
  ContourStyle -> {{Thickness[0.005], Dashed, 
     Opacity[1]}, {Thickness[0.005], Dashed, Opacity[1]}}, 
  RegionFunction -> Function[{x, y, z}, x <= 0 || y <= 0]]]

yielding:

enter image description here

Edit:

This may not be the most elegant solution, but:

Show[ContourPlot[
  PDF[MultinormalDistribution[{0, 0}, {{2, 1/2}, {1/2, 1}}], {x, 
    y}], {x, -2, 2}, {y, -2, 2}, Contours -> {0.05, 0.09}, 
  ContourShading -> {None, Green}, 
  ContourStyle -> {{Thickness[0.004], Opacity[1]}, {Thickness[0.004], 
     Opacity[1]}}], 
 ContourPlot[
  PDF[MultinormalDistribution[{0, 0}, {{2, 1/2}, {1/2, 1}}], {x, 
    y}], {x, -2, 2}, {y, -2, 2}, Contours -> {0.06, 0.07}, 
  ContourShading -> {None, Yellow}, 
  ContourStyle -> {{Thickness[0.005], Dashed, 
     Opacity[1]}, {Thickness[0.005], Dashed, Opacity[1]}}], 
 ListLinePlot[{{0, 
    FindRoot[
      Evaluate[
        PDF[MultinormalDistribution[{0, 0}, {{2, 1/2}, {1/2, 1}}], {x,
            y}] /. x -> 0] == .05, {y, 1}][[1, 2]]}, {0, 
    FindRoot[
      Evaluate[
        PDF[MultinormalDistribution[{0, 0}, {{2, 1/2}, {1/2, 1}}], {x,
            y}] /. x -> 0] == .09, {y, 1}][[1, 2]]}}, 
  PlotStyle -> Directive[Black, AbsoluteThickness[2]]], 
 ListLinePlot[{{FindRoot[
      Evaluate[
        PDF[MultinormalDistribution[{0, 0}, {{2, 1/2}, {1/2, 1}}], {x,
            y}] /. y -> 0] == .09, {x, 1}][[1, 2]], 
    0}, {FindRoot[
      Evaluate[
        PDF[MultinormalDistribution[{0, 0}, {{2, 1/2}, {1/2, 1}}], {x,
            y}] /. y -> 0] == .05, {x, 1}][[1, 2]], 0}}, 
  PlotStyle -> Directive[Black, AbsoluteThickness[2]]], 
 ContourPlot[Sin[x*10 + y*10] == 0, {x, -2, 2}, {y, -2, 2}, 
  ContourStyle -> Directive[Black, AbsoluteThickness[2]], 
  RegionFunction -> 
   Function[{x, y, 
     z}, .05 <= 
      PDF[MultinormalDistribution[{0, 0}, {{2, 1/2}, {1/2, 1}}], {x, 
        y}] <= .09 && (x <= 0 || y <= 0)]]]

gives:

enter image description here

$\endgroup$
  • $\begingroup$ Thank you, but this is not exactly what I want. I would like to show the cutted are but with a texture made of diagonal lines, as shown in the figure on the right $\endgroup$ – apt45 Aug 31 '16 at 19:52
1
$\begingroup$

This can all be done with a single call to ContourPlot[] and the clever use of MeshFunctions:

mnpdf[x_, y_] = PDF[MultinormalDistribution[{0, 0}, {{2, 1/2}, {1/2, 1}}], {x, y}];

ContourPlot[mnpdf[x, y], {x, -2, 2}, {y, -2, 2}, Contours -> {0.05, 0.06, 0.07, 0.09}, 
            ContourShading -> {None, Green, Yellow, Green}, 
            ContourStyle -> {Directive[Thickness[0.002], Opacity[1]], 
                             Directive[Thickness[0.005], Dashed, Opacity[1]], 
                             Directive[Thickness[0.005], Dashed, Opacity[1]]}, Mesh -> 25, 
            MeshFunctions -> {(#1 + #2) Boole[! (#1 > 0 && #2 > 0) && 
                              0.05 < mnpdf[#1, #2] < 0.09] &}, MeshStyle -> Opacity[1]]

hatched and shaded contour plot

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.