[Matplotlib-users] Issues Removing Paths from a filled polygon

Carlos Baumann CBaumann at slb.com
Tue Sep 17 23:36:55 EDT 2019


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: http://matplotlib.1069221.n5.nabble.com/removing-paths-inside-polygon-td40632.html

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.

  1.  "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 !
    # https://matplotlib.org/3.1.1/gallery/event_handling/pick_event_demo2.html

    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),
                      copy.deepcopy(exterior.codes[:]))
exterior.vertices *= 4
exterior.vertices -= 2

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

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

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

clip_path = mpath.Path(vertices, codes)
star = mpath.Path.unit_regular_star(6)
star = mpath.Path(copy.deepcopy(star.vertices),
                  copy.deepcopy(star.codes[:]))
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.add_collection(col)
ax.set_title('Clip path')
ax.autoscale()

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

# 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.add_collection(col)
ax.set_title('Target polygon clipped by clip_path')
ax.autoscale()

plt.tight_layout()
plt.show()

[cid:image001.png at 01D56DA8.4FE293C0]
[cid:image002.png at 01D56DA8.4FE293C0]
[cid:image003.png at 01D56DA8.4FE293C0]


Schlumberger-Private
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190918/fb8add9b/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 42013 bytes
Desc: image001.png
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190918/fb8add9b/attachment-0003.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.png
Type: image/png
Size: 42963 bytes
Desc: image002.png
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190918/fb8add9b/attachment-0004.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image003.png
Type: image/png
Size: 41275 bytes
Desc: image003.png
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190918/fb8add9b/attachment-0005.png>


More information about the Matplotlib-users mailing list