I have a specific pattern to show in Graphics and I would like to choose the PlotRegion as a shape. In other words, I would like to show part of the pattern within a specific shape such as Hexagon or Polygon.

Assume that the pattern is Rectangle, see below, and I would like to make the PlotRegion to be a Hexagon. How can I do that?

Graphics[{Pink, Rectangle[]}]

enter image description here


Use Transparent as a color and make a mask using it. Combine different Graphics with Show or combine the lists of graphics primitives.

Note, that this can also be done with continues Opacity and not only sharp cutoff. For example Transparent can be used in the ColorFunction of DensityPlot and then your mask can be described by a continues value function.

The mask can be constructed using RegionDifference

Show[{Graphics[{Pink, Rectangle[]}], 
   RegionDifference[Rectangle[], Disk[{0.5, 0.5}, 0.3]], 
   BaseStyle -> Directive[EdgeForm[None], FaceForm[White]]]}]

enter image description here

If the underlying graphics you mask is a Plot-like and not a general Graphcis, one can use MeshRegion and RegionFunction. See the related Q&A.

  • $\begingroup$ Can you please put an example even for a circle shape? $\endgroup$ Sep 14 '18 at 12:43
  • 1
    $\begingroup$ @Abdullah please see the updated example. $\endgroup$
    – Johu
    Sep 14 '18 at 12:59

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.