1
$\begingroup$

I want these two contours to be plotted in a single figure and filled the inner regions of each contour with different colors. The problem that I am facing is the larger contour shading overshadows the smaller one. I want the inner region to be red (for x1) and the rest of the inner region for the larger contour (for x2) to be blue. How do I achieve that?

x1 = ContourPlot[{a^2 + b^2 + a*b}, {a, -7, 7}, {b, -7, 7}, Contours -> {4},
                    ContourShading -> {Red, None}, MaxRecursion -> 5, 
                   Epilog -> {Black, PointSize[0.015], Point[{0, 0}]}, 
                   ContourStyle -> {Red}, Frame -> True, FrameLabel -> {"a", "b"}];
                 
x2 = ContourPlot[{a^2 + b^2}, {a, -7, 7}, {b, -7, 7}, Contours -> {9},
                     ContourShading -> {Blue, None}, MaxRecursion -> 5, 
                    Epilog -> {Black, PointSize[0.015], Point[{0, 0}]}, 
                    ContourStyle -> {Blue}, Frame -> True, FrameLabel -> {"a", "b"}]
                  
x4 = Show[{x1, x2}]
$\endgroup$
5
  • 1
    $\begingroup$ Why aren't you using RegionPlot[]? RegionPlot[{a^2 + b^2 <= 9, a^2 + b^2 + a b <= 4}, {a, -7, 7}, {b, -7, 7}, BoundaryStyle -> Blue, Epilog -> {Black, PointSize[0.015], Point[{0, 0}]}, PlotStyle -> {Blue, Red}] $\endgroup$ Jul 7, 2022 at 22:02
  • 1
    $\begingroup$ Reverse the order in the Show: x4 = Show[{x2, x1}] $\endgroup$
    – Bob Hanlon
    Jul 7, 2022 at 22:30
  • $\begingroup$ @J.M. It is working. Thank you so much for your suggestion. $\endgroup$ Jul 7, 2022 at 22:45
  • $\begingroup$ @BobHanlon, It is working. Thank you so much. $\endgroup$ Jul 7, 2022 at 22:46
  • $\begingroup$ @J.M., One more question. According to your suggestion, both regions are enclosed by blue boundaries. Could you please tell me how to put two different color boundaries in the two different regions? Say, for the inner one black and for the outer one green. $\endgroup$ Jul 7, 2022 at 22:55

1 Answer 1

2
$\begingroup$

Edit

{pts1, pts2} = 
  Cases[ContourPlot[{a^2 + b^2 + a*b == 4, a^2 + b^2 == 9}, {a, -7, 
      7}, {b, -7, 7}] // Normal, Line[pts_] :> pts, Infinity];
Graphics[{{Red, Polygon[pts1]}, {Blue, Polygon[pts2 -> pts1]}, {Green,
    AbsoluteThickness[2], Line@pts1}, {AbsoluteThickness[2], 
   Line@pts2}, {AbsolutePointSize[5], Point[{0, 0}]}}, Frame -> True]

enter image description here

Original

Another way is use BoundaryDiscretizeGraphics to draw the gap between the two contours to blue.

x1 = ContourPlot[{a^2 + b^2 + a*b}, {a, -7, 7}, {b, -7, 7}, 
   Contours -> {4}, ContourShading -> {Red, None}, MaxRecursion -> 5, 
   Epilog -> {Black, PointSize[0.015], Point[{0, 0}]}, 
   ContourStyle -> Directive[Thick, Green], Frame -> True, 
   FrameLabel -> {"a", "b"}];
plot1 = ContourPlot[{a^2 + b^2 + a*b == 4}, {a, -7, 7}, {b, -7, 7}, 
   ContourStyle -> Green];
plot2 = ContourPlot[a^2 + b^2 == 9, {a, -7, 7}, {b, -7, 7}, 
   ContourStyle -> Black];
plot12 = 
 Graphics[{Blue, 
   BoundaryDiscretizeGraphics[
    ContourPlot[{a^2 + b^2 + a*b == 4, a^2 + b^2 == 9}, {a, -7, 
      7}, {b, -7, 7}]]}]

enter image description here

Show[x1, plot12, plot1, plot2, PlotRange -> 4]

enter image description here

$\endgroup$
1
  • $\begingroup$ It worked. Thank you so much. $\endgroup$ Jul 8, 2022 at 8:09

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.