|
|
images_contours_and_fields_Examples 03_image_demo_clip_path. |
H.Kamifuji . |
円形のパッチで切り取られた画像のデモ。""" 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 image_file = cbook.get_sample_data('grace_hopper.png') 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() |
![]() Python 3.11.2 見直しました。上記のコードでは、下記のエラーが発生します。 Traceback (most recent call last): File "_:\image_demo_clip_path.py", line 9, in <module> image_file = cbook.get_sample_data('grace_hopper.png') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\_____\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib\cbook\__init__.py", line 545, in get_sample_data return path.open('rb') ^^^^^^^^^^^^^^^ File "C:\Users\_____\AppData\Local\Programs\Python\Python311\Lib\pathlib.py", line 1044, in open return io.open(self, mode, buffering, encoding, errors, newline) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\_____\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\matplotlib\\mpl-data\\sample_data\\grace_hopper.png' sample_data\grace_hopper.png がインストールされていない、matplotlib 内部のエラーのようです。matplotlib の改修(先祖帰りバグの改修)を待つしかない。 Python 3.11.6 (matplotlib 3.7.1) では、下記のようなエラーがあり、実行できない。 Traceback (most recent call last): File "M:\____\image_demo_clip_path.py", line 9, inPython 3.12.0 (matplotlib 3.8.1) では、下記のようなエラーがあり、実行できない。 Traceback (most recent call last): File "E:\______\image_demo_clip_path.py", line 9, inPython 3.11.6 (matplotlib 3.7.1) 及び Python 3.12.0 (matplotlib 3.8.1) 共に、site-packages 内に、ada.png ファイルが存在しないため実行できないので、ローカルのファイルを、直に参照するように、書き換えました。 """ 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 # image_file = cbook.get_sample_data('grace_hopper.png') image_file = './grace_hopper_2.jpg' 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()Python 3.11.6 (matplotlib 3.7.1) 及び Python 3.12.0 (matplotlib 3.8.1) 共に、正常実行できる。 ![]() |
images_contours_and_fields_Examples code: image_demo_clip_path.py |
|