I need to draw a contour figure defined by coordinate x and y. The domain is a rectangle (-100<=x<=100,-100<=y<=100) excluding a circle (center at the origin, and radius of 5). The object function is 'z=x+y'.

What confuses me is how to exclude the circle from the rectangle. How can I draw such a contour figure?

up vote 2 down vote accepted

You can also use ConditionalExpression as the first argument of ContourPlot:

ContourPlot[ConditionalExpression[x + y, x^2 + y^2 >= 5^2],
  {x, -100, 100}, {y, -100, 100},  PlotLegends -> Automatic]

enter image description here

How about this?

ContourPlot[x + y, {x, -100, 100}, {y, -100, 100}, 
 RegionFunction -> Function[{x, y, z}, x^2 + y^2 >= 5^2], 
 PlotPoints -> 100, PerformanceGoal -> "Quality", 
 PlotLegends -> Automatic, ColorFunction -> "ThermometerColors"]

enter image description here

If the smoothness of disk bothers you (it bothers me), you can cheat it like so:

Show[ContourPlot[x + y, {x, -100, 100}, {y, -100, 100}, 
  PlotPoints -> 100, PerformanceGoal -> "Quality", 
  PlotLegends -> Automatic, ColorFunction -> "ThermometerColors"], 
 Graphics[{White, Disk[{0, 0}, 5]}]]

enter image description here

  • Nice quality of the figure! Got it~ Thank you! – Robin_Lyn Nov 15 at 6:07

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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