3
$\begingroup$

I have a simple question I have no answer to - how do I create an Epilog in (for example) a DensityPlot that consists of a Disk[] with a hole with the inner radius being r1 and the outer radius being r2? Imagine something like this:

Graphics[{{Gray, Disk[{0, 0}, 1]}, {White, Disk[{0, 0}, 0.5]}}]

However, this would hide the density plot in the "hole" (it would stay white). How to create such object as an epilog to a density plot, stream plot etc?

I had two ideas: first, if there is something like GraphicsDifference, that would be it: bigger Disk minus the smaller one creates the object I'm after. However, I found no such function.

Second: create a circle using a really thick line so that the inner boundary corresponds to r1 and the outer to r2. However this would not scale nicely (i.e. using ImageSize -> ... during export would not preserve these radii).

Thanks.

$\endgroup$
3
  • 4
    $\begingroup$ Annulus was introduced in 10.2 $\endgroup$
    – Kuba
    May 9, 2018 at 20:32
  • $\begingroup$ Thanks, that's great! Exactly what I was looking for! I googled a lot of queries like "Disk with a hole", but nothing returned Annulus...I am sorry though. Just curious: how would you make a graphics with rectangular hole so that the hole is transparent (the plot is visible through it)? So as to keep to the spirit of the original question... $\endgroup$
    – user16320
    May 9, 2018 at 20:36
  • $\begingroup$ A disk with a rectangular hole or a rectangle? $\endgroup$
    – Kuba
    May 9, 2018 at 20:46

3 Answers 3

9
$\begingroup$

use region tools to construct more general figures (addressing comment):

r = RegionDifference[ Disk[{8, 8}, 2], Rectangle[{7, 7}, {9, 8}]];
Show[
 ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi}], 
 RegionPlot[r, Frame -> False, PlotStyle -> Red, 
  BoundaryStyle -> Black]]

or

ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi}, 
 Epilog -> 
  First@RegionPlot[r, Frame -> False, PlotStyle -> Red, 
    BoundaryStyle -> Black]]

enter image description here

$\endgroup$
1
  • $\begingroup$ This is what I've been looking for! Thank you. $\endgroup$
    – user16320
    May 9, 2018 at 22:20
5
$\begingroup$

Nowadays, Polygon[] supports holes. Using george's example:

hole = Polygon[MeshPrimitives[BoundaryDiscretizeGraphics[Disk[{8, 8}, 2]],
                              2][[1, 1]] -> {{7, 7}, {9, 7}, {9, 8}, {7, 8}}];

ContourPlot[Cos[x] + Cos[y], {x, 0, 4 π}, {y, 0, 4 π}, 
            Epilog -> {Directive[Red, EdgeForm[Directive[Thick, Black]]], hole}]

disk with hole example

$\endgroup$
2
$\begingroup$

Use BezierCurve and FilledCurve. Just for fun.

c = 4/3 Tan[π/8]; 
pts = {{0, 1}, {c, 1}, {1, c}, {1, 
   0}, {1, -c}, {c, -1}, {0, -1}, {-c, -1}, {-1, -c}, {-1, 0}, {-1, 
   c}, {-c, 1}, {0, 1}};
Graphics[{Orange, 
  FilledCurve[{{BezierCurve[3 pts]}, {BezierCurve[
      2 pts]}, {BezierCurve[pts]}}]}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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