我加载了漂亮的文件(这些文件也是从.pack CT扫描转换而来的)。我的目标是使用阈值otsu算法将其从背景中屏蔽并比较两个图像。当我尝试绘图时出现错误
AttributeError: 'AxesSubplot' object has no attribute 'ravel'
下面是代码,并随附了屏幕截图。
import SimpleITK as sitk
import matplotlib.pyplot as plt
import numpy as np
from skimage.filters import threshold_otsu
#THRESHOLD OTSU
img = sitk.GetArrayFromImage(sitk.ReadImage("\\\\x.x.x.x/users/ddff/python/nifts/prr_ipsi.nii"))
print(img.shape)
thresh = threshold_otsu(img.flatten())
#thresh = thresh.reshape(img.shape)
binary = img <= thresh
#I can plot this image slice fine
plt.imshow(img[20,:,:])
fig, axes = plt.subplots(ncols=1)
ax = axes.ravel()
ax[0] = plt.subplot(1, 3, 1)
ax[1] = plt.subplot(1, 3, 2)
ax[2] = plt.subplot(1, 3, 3, sharex=ax[0], sharey=ax[0])
ax[0].imshow(img[20,:,:], cmap=plt.cm.gray)
ax[0].set_title('Original Breast Delineation')
ax[0].axis('off')
ax[1].hist(thresh, bins=256)
ax[1].set_title('Histogram ')
ax[1].axvline(thresh, color='r')
ax[2].imshow(binary[20,:,:], cmap=plt.cm.gray)
ax[2].set_title('Thresholded')
ax[2].axis('off')
plt.show()[enter image description here][1]
axes
只是一个有1列的单一数字,所以没有任何要ravel
或flatten
。如果您有多个子图,它将起作用。但是,ravel
如果您只有一行或一列,则可以执行以下操作。
fig, ax = plt.subplots(ncols=3, sharex=True, sharey=True)
ax[0].imshow(img[20,:,:], cmap=plt.cm.gray)
ax[0].set_title('Original Breast Delineation')
ax[0].axis('off')
ax[1].hist(thresh, bins=256)
ax[1].set_title('Histogram ')
ax[1].axvline(thresh, color='r')
ax[2].imshow(binary[20,:,:], cmap=plt.cm.gray)
ax[2].set_title('Thresholded')
ax[2].axis('off')
如果您想要2d子图实例的矩阵,则可以使用Thomas Kühn
的建议。
fig, ax = plt.subplots(ncols=3, sharex=True, sharey=True, squeeze=False)
然后您可以按以下方式访问子图
ax[0][0].imshow()
ax[0][1].imshow()
......
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句