Re: [Matplotlib-users] removing paths inside polygon

This is an example that I reviewed in my effort to draw a polygon with
certain zones masked, or clipped.
I have tried to find a way to Mask polygonal areas before filling/drawing,
but so far I have not found what I really need.
I need to be able to fill plot areas without touching certain polygonal
areas that may be totally or partially where the polygon will be filled.
I cannot fill with white the polygonal areas after rendering because I would
be deleting information (e.g. colors and zorder) in those areas.
Can I copy the information in those polygonal areas, and then copy them back
after filling/drawing what I needed ?

Here are the bugs issues that I found:
This example was found at:

While doing this, I stumbled on a bug and a couple of issues:
1) When .set_clip_path() is used, the "Zoom to rectangle" and "Pan"
functions can move draw the plots outside the subplot.
See the attached 3 snapshots.
2) “Pick events” are generated even for the "clipped" areas of the star. It
would be nice if this was not so.

import matplotlib.pyplot as plt
import matplotlib.path as mpath
import matplotlib.collections as mcol
import numpy as np
import copy

pick = 0
def on_pick(event):
    global pick
    # however pick events are NOT generated while the Zoom or Translate
options are on !



    pick += 1
    #print("on_pick ",pick,": ", str(event.artist), " event=", str(event))
    print("on_pick ",pick,": zorder:", event.artist.get_zorder())

exterior = mpath.Path.unit_rectangle()
exterior = mpath.Path(copy.deepcopy(exterior.vertices),
exterior.vertices *= 4
exterior.vertices -= 2

#interior = mpath.Path.unit_circle()
interior = mpath.Path.unit_rectangle()
interior = mpath.Path(copy.deepcopy(interior.vertices),
interior.vertices = interior.vertices[::-1] # reverse the list

vertices = np.concatenate([exterior.vertices, interior.vertices])
codes = np.concatenate([,])

interior = mpath.Path.unit_rectangle()
interior = mpath.Path(copy.deepcopy(interior.vertices),
interior.vertices = interior.vertices[::-1]
interior.vertices -= 0.6
vertices = np.concatenate([vertices, interior.vertices])
codes = np.concatenate([codes,])

clip_path = mpath.Path(vertices, codes)
star = mpath.Path.unit_regular_star(6)
star = mpath.Path(copy.deepcopy(star.vertices),
star.vertices *= 3.5

figu, ax = plt.subplots(figsize=(6,6), dpi=100)
cbid = figu.canvas.mpl_connect("pick_event", on_pick)

ax = plt.subplot(221)
col = mcol.PathCollection([clip_path], facecolor='green')
ax.set_title('Clip path')

ax = plt.subplot(222)
col = mcol.PathCollection([star], facecolor='red')
ax.set_title('Target polygon')

# BUG: "zoom to rectangle" on this subplot shows part of the star outside
this subplot ?
# BUG: "Pan" on the clipped star can be translated to anywhere over the
other subplots ?
# "Zoom to rect" and "Pan" work as expected on the two top subplots.
# This is an issue associated with set_clip_path(), when the line below is
commented, Zoom / Pan behave as expected.
# NOTE: pick events are generated even for the "clipped" areas of the star !
It would be nice if this was not so.
ax = plt.subplot(2,2,4)
col = mcol.PathCollection([star])
col.set_clip_path(clip_path, ax.transData)
col.set(zorder=9, facecolor="blue", picker=True)
ax.set_title('Target polygon clipped by clip_path')


Sent from:
Matplotlib-users mailing list