Fix image with multiple patches in matplotlib

I have a plot in pylab that I want to pin to the borders of a UK map.

I also made a series of patches that contain the outlines of each country: one for England, one for Wales, etc.

Cutting a chart with one patch works brilliantly:

fig = plt.figure()
ax = fig.add_subplot(111)
im = ax.scatter(x,y,c = z)
ax.add_patch(patch)
im.set_clip_path(patch)

But if I try to do it more than one, it leaves me with nothing - it’s understandable, because not a single part of the plot is inside each country at the same time.

Does anyone know how I can crop using an operator like OR? (i.e. do not pinch if inside this patch or this, etc.).

+5
source share
1 answer

, , , (, , ..). , , "- , " OR "?", :

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches

np.random.seed(101)
x = np.random.random(100)
y = np.random.random(100)

fig = plt.figure()
ax = fig.add_subplot(111)
imForEngland = ax.scatter(x,y)
fig.savefig('beforeclip.png')
imForWales = ax.scatter(x,y)
england = patches.Circle((.75,.75),radius=.25,fc='none')
wales = patches.Circle((.25,.25),radius=.25,fc='none')
ax.add_patch(england)
ax.add_patch(wales)
imForEngland.set_clip_path(england)
imForWales.set_clip_path(wales)

fig.savefig('afterclip.png')

: enter image description here : enter image description here

+6

All Articles