Suppose I am plotting some two-dimensional function, using DensityPlot, and that I would like to add the outer line of a circle to this figure. I found one way to do it:

pt1 = DensityPlot[x y, {x, -5, 5}, {y, -5, 5}];
pt2 = RegionPlot[x^2 + y^2 < 1, {x, -5, 5}, {y, -5, 5}, 
BoundaryStyle -> Black, PlotStyle -> White];
Show[pt1, pt2]

However, 1) I suspect that this can be done in an easier way; 2) The inner part of the circle is white now, whereas I would like to so the function value of the first figure in the center. Any ideas on how to do this?

EDIT: more generally, I would like to plot a function which can become zero, e.g. sin[x]sin[y] using DensityPlot and then plot a black line connecting all the points where this is zero. Any thoughts on how to do this?


In general for adding a graphics overlay to a plot your best options is Epilog:

DensityPlot[x y, {x, -5, 5}, {y, -5, 5}, Epilog -> Circle[{0, 0}, 1]]

enter image description here

For your more specific case you could plot the zero contour using ContourPlot and pass that to Epilog:

ZeroDensityPlot[f_, args__] := 
 DensityPlot[f, args, 
  Epilog -> First@ContourPlot[f == 0, args, ContourStyle -> Black]

ZeroDensityPlot[Sin[x] Sin[y], {x, -5, 5}, {y, -5, 5}]

enter image description here

| improve this answer | |

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.