python 3.8.10, ubuntu 20.04, vsc 我想contourf在剪裁成圓形補丁后做一個。好像拿不到 補丁應用正確,但我無法弄清楚如何做輪廓。錯誤在等高線上:
float() 引數必須是字串或數字,而不是 'AxesImage'
def contour_calc(img):
#img is np 1832x1832 greyscale 8 bit
fig, ax = plt.subplots()
im = ax.imshow(img)
patch = patches.Circle((916, 916), radius=900, transform=ax.transData)
im.set_clip_path(patch)
ax.axis('off')
#plt.show() # works perfectly, patch is applied
contourf(im,levels=2, colors=['k','w'])
plt.show()
完整的錯誤跟蹤:
Traceback (most recent call last):
File "/home/cliff/Documents/vsc_projects/tychocam_main/mainApp/cloud_processing/contours.py", line 24, in contour_calc
ax.contourf(im,levels=2, colors=['k','w'])
File "/home/cliff/.local/lib/python3.8/site-packages/matplotlib/__init__.py", line 1412, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "/home/cliff/.local/lib/python3.8/site-packages/matplotlib/axes/_axes.py", line 6271, in contourf
contours = mcontour.QuadContourSet(self, *args, **kwargs)
File "/home/cliff/.local/lib/python3.8/site-packages/matplotlib/contour.py", line 812, in __init__
kwargs = self._process_args(*args, **kwargs)
File "/home/cliff/.local/lib/python3.8/site-packages/matplotlib/contour.py", line 1441, in _process_args
x, y, z = self._contour_args(args, kwargs)
File "/home/cliff/.local/lib/python3.8/site-packages/matplotlib/contour.py", line 1476, in _contour_args
z = ma.asarray(args[0], dtype=np.float64)
File "/home/cliff/.local/lib/python3.8/site-packages/numpy/ma/core.py", line 7952, in asarray
return masked_array(a, dtype=dtype, copy=False, keep_mask=True,
File "/home/cliff/.local/lib/python3.8/site-packages/numpy/ma/core.py", line 2829, in __new__
_data = np.array(data, dtype=dtype, copy=copy,
TypeError: float() argument must be a string or a number, not 'AxesImage'
uj5u.com熱心網友回復:
您可以創建一個 numpy 掩碼來過濾掉圓圈區域之外的所有內容:
from matplotlib import pyplot as plt
from matplotlib import patches
import numpy as np
from scipy.ndimage import gaussian_filter
# create a test image
img = gaussian_filter(np.random.rand(1832, 1832), 50)
img -= img.min()
img *= 255 / img.max()
img = img.astype(np.uint8)
# mask away everything outside the circle area
x, y = np.meshgrid(np.arange(img.shape[1]), np.arange(img.shape[0]))
xm, ym = 916, 916
rad = 900
img_ma = np.ma.array(img, mask=(x - xm) ** 2 (y - ym) ** 2 > rad ** 2)
fig, ax = plt.subplots()
ax.contourf(img_ma, levels=2, colors=['crimson', 'gold'])
ax.axis('off')
ax.set_aspect('equal') # show a circle as a circle, not as an ellipse
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/439813.html
標籤:Python 麻木的 matplotlib 面具
下一篇:Pytorch自定義資料加載器:TypeError:pic應該是PILImage或ndarray。得到<class'torch.Tensor'>
