I'm looking for advice on how to create a plot using Matplotlib patches where the transparency can be applied uniformly to all patches. Specifically, if I have overlapping patches, I would like the alpha
value to be applied to the union of the two patches, rather than applied individually. The intersection region should look the same as the the individual structures and if there are differences in the patch definition (such as color), the last patch added to the collection should take precedence.
Below is a simple example of what doesn't work.
import matplotlib.pylab as plt
import matplotlib as mpl
f, (ax1, ax2) = plt.subplots(1, 2, tight_layout=True)
# assign alpha to individual patches
patch1 = mpl.patches.Rectangle((0.4, 0.4), .5, .5, alpha=0.5)
patch2 = mpl.patches.Rectangle((0.1, 0.1), .5, .5, alpha=0.5)
ax1.add_patch(patch1)
ax1.add_patch(patch2)
ax1.set_title('individual patches')
# try assigning alpha to collection
patch3 = mpl.patches.Rectangle((0.4, 0.4), .5, .5)
patch4 = mpl.patches.Rectangle((0.1, 0.1), .5, .5)
collection = mpl.collections.PatchCollection([patch3, patch4], alpha=0.5)
ax2.add_collection(collection)
ax2.set_title('patch collection')
# overlap region is darker
plt.show()
Based on some other discussions online, I have looked into some other techniques, such rendering an image from the intersection with alpha=1
and then plotting this image with alpha < 1
, but because the image would be quite large in my application, I'd prefer to use geometric primitives, such as Patches.
Any ideas on how to make this work?