Clipping images with patches

Demo of image that's been clipped by a circular patch.

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.cbook as cbook


with cbook.get_sample_data('grace_hopper.png') as image_file:
    image = plt.imread(image_file)

fig, ax = plt.subplots()
im = ax.imshow(image)
patch = patches.Circle((260, 200), radius=200, transform=ax.transData)
im.set_clip_path(patch)

ax.axis('off')
plt.show()
../../_images/sphx_glr_image_clip_path_001.png

Out:

/build/matplotlib-Aszjp3/matplotlib-3.2.0/examples/images_contours_and_fields/image_clip_path.py:13: MatplotlibDeprecationWarning:
The MATPLOTLIBDATA environment variable was deprecated in Matplotlib 3.1 and will be removed in 3.3.
  with cbook.get_sample_data('grace_hopper.png') as image_file:

References

The use of the following functions and methods is shown in this example:

import matplotlib
matplotlib.axes.Axes.imshow
matplotlib.pyplot.imshow
matplotlib.artist.Artist.set_clip_path

Out:

<function Artist.set_clip_path at 0x7fa64a754430>

Keywords: matplotlib code example, codex, python plot, pyplot Gallery generated by Sphinx-Gallery