【发布时间】:2020-10-28 20:11:44
【问题描述】:
我尝试运行一个 Python 程序来绘制给定图像的直方图。它的行为符合预期,但会产生以下错误:
axes[k,idx].hist(x=fruit[:, :, i].flat, bins=256, range=(0, 255)
IndexError: index 4 is out of bounds for axis 1 with size 4
这是我的代码:
import numpy
import numpy as np
import skimage.io
import matplotlib.pyplot
import matplotlib.pyplot as plt
raspberry = skimage.io.imread(fname="raspberry.jpg", as_gray=False)
apple = skimage.io.imread(fname="apple.jpg", as_gray=False)
fruits_data = [apple,raspberry]
fig, axes = plt.subplots(nrows=2, ncols=4)
ax0, ax1, ax2, ax3, ax4, ax5, ax6, ax7 = axes.flatten()
axs = [ax0, ax1, ax2, ax3, ax4, ax5, ax6, ax7]
i = 0
k=0
for j in range(0, 6, 4):
for fruit in fruits_data:
for idx in range(j,j+4):
if i < 3:
axes[k,idx].hist(x=fruit[:, :, i].flat, bins=256, range=(0, 255))
i += 1
else :
i = 0
axes[k,idx].imshow(fruit)
k = 1
plt.show()
【问题讨论】:
标签: python numpy matplotlib image-processing