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

up vote 1 down vote accepted

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[]}], 
  DiscretizeRegion[
   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.

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

Your Answer

 
discard

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.