Re move, mask, or hide parts of a polygon?

I've become stuck on what seems to be an easy problem.

Can part of a patch or collection be masked or otherwise hidden?

I assumed that my_polygon.set_clip_path( patch ) where patch is a
patches.Polygon would do the trick.

Thanks,
-Erik

···


View this message in context: http://old.nabble.com/Remove%2C-mask%2C-or-hide-parts-of-a-polygon--tp27842142p27842142.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Please post a complete example that demonstrate your problem. My guess
is that maybe you are not setting the transform of the clipping path
correctly.
Regards,

-JJ

···

On Tue, Mar 9, 2010 at 4:22 PM, othererik <othererik@...287...> wrote:

I assumed that my_polygon.set_clip_path( patch ) where patch is a
patches.Polygon would do the trick.

Jae-Joon Lee wrote:

Please post a complete example that demonstrate your problem. My guess
is that maybe you are not setting the transform of the clipping path
correctly.
Regards,

-JJ

Here's a short example that does the opposite of what I'm looking for. The
goal is to take a the polygon "poly_patch" and "cut" regions out of it.
All I've managed to achieve so far has been to show regions of the
"poly_patch".

I'm apparently missing a detail somewhere. Thanks for the help!

···

---

import matplotlib
from matplotlib import patches
import pylab

fig=pylab.figure()
ax=fig.add_subplot(111)

# background line
line = ax.plot( [ 1, 2, 3], [ 1, 2, 3 ] )

# part I would like to see through
hole_patch = patches.Circle((2,2), radius=.5, facecolor='none')

# mask polygon that should cover/hide the area not intersecting the
"hole_patch"
poly_patch = patches.Polygon(((1,1),(1,3),(3,3), (3,1)))
ax.add_patch( hole_patch )
ax.add_patch( poly_patch )

# gets the opposite effect intended
poly_patch.set_clip_path( hole_patch )

pylab.show()

View this message in context: http://old.nabble.com/Remove%2C-mask%2C-or-hide-parts-of-a-polygon--tp27842142p27874544.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

With clip_path set, things inside the clip_path is only drawn. So, I
think you're misusing clip_path.
To draw a rectangle with a hole in it, you need to create a compound
path. And, this requires some understanding of how path system works
in general.

See this,

http://matplotlib.sourceforge.net/users/path_tutorial.html

and

http://old.nabble.com/Compound-paths-to19278422.html#a19296862

Here is modified version of your script that draws a rectangle with a
hole. It concatenates two existing paths. But, often it is better to
start from scratch (but again you need to understand how path works).
And of course, the resulting compound path can be set as a clip_path
of other artist.

Regards,

-JJ

import matplotlib
from matplotlib import patches
import pylab

fig=pylab.figure()
ax=fig.add_subplot(111)

# background line
line = ax.plot( [ 1, 2, 3], [ 1, 2, 3 ] )

# mask polygon that should cover/hide the area not intersecting the
poly_patch = patches.Polygon(((1,1),(1,3),(3,3), (3,1)))
# part I would like to see through
hole_patch = patches.Circle((2,2), radius=.5, facecolor='red')

from matplotlib.bezier import make_path_regular, concatenate_paths
from matplotlib.path import Path
from matplotlib.patches import PathPatch

poly_path = make_path_regular(poly_patch.get_path())

# close the path
closed_polygon_path = Path(list(poly_path.vertices)+[(0,0)],
                           list(poly_path.codes)+[Path.CLOSEPOLY])

# circle with coordinate transformed
hole_path = hole_patch.get_path()
hole_path_transformed =
hole_patch.get_patch_transform().transform_path(hole_path)

# make polygon with a hole
poly_with_hole_path = concatenate_paths([closed_polygon_path,
                                         hole_path_transformed])

poly_with_hole_patch = PathPatch(poly_with_hole_path)

ax.add_patch(poly_with_hole_patch)

ax.set_xlim(0, 4)
ax.set_ylim(0, 4)

pylab.show()

···

On Fri, Mar 12, 2010 at 7:45 AM, othererik <othererik@...287...> wrote:

Here's a short example that does the opposite of what I'm looking for. The
goal is to take a the polygon "poly_patch" and "cut" regions out of it.
All I've managed to achieve so far has been to show regions of the
"poly_patch".