【发布时间】:2019-08-30 16:24:47
【问题描述】:
我正在比较 python 中的图像直方图。那是当我注意到同一张图像的直方图,用 pyplot 的 subplots 绘制两次,根据 subplots 索引可能看起来不同。这是一个最小的例子:
fig, (ax0, ax1) = plt.subplots(nrows = 2, ncols = 2, figsize = (15,15))
img1 = blocks_1[0][0]
img2 = blocks_1[0][0]
ax0[0].imshow(img1)
ax1[0].imshow(img2)
hist3, bins3 = np.histogram(img1, bins=255)
hist4, bins4 = np.histogram(img2, bins=255)
ax0[1].bar(bins3[:-1], hist3)
ax1[1].bar(bins4[:-1], hist4)
如果我这样绘制,我的 jupyter notebook 中的两个直方图看起来会有所不同:
fig, (ax0, ax1) = plt.subplots(nrows = 2, ncols = 2, figsize = (10,10))
img1 = blocks_1[0][0]
img2 = blocks_1[0][0]
ax0[0].imshow(img1)
ax0[1].imshow(img2)
hist3, bins3 = np.histogram(img1, bins=255)
hist4, bins4 = np.histogram(img2, bins=255)
ax1[0].bar(bins3[:-1], hist3)
ax1[1].bar(bins4[:-1], hist4)
怎么会这样?这是显示不同直方图的图像。它们甚至没有镜像。
【问题讨论】:
-
了解您的数据(即
blocks_1)的样子会很有帮助,因为我无法使用随机数据重现错误 -
啊,我明白了。问题似乎在于
plt.bar()。如果您尝试绘制相同的直方图两次,它甚至会出现(即ax1[0].bar(bins3[:-1], hist3) ax1[1].bar(bins3[:-1], hist3)。这似乎是 pyplot 方面的一个错误?!作为一种解决方法,您可以简单地使用plt.hist而不是np.histogram和@987654330 @ -
我已经打开了一个 ;)
-
:该死的,太慢了。顺便说一句,如果这真的是一个错误,那就给它一个答案。我会接受的。
-
我标记为类似问题的重复,其中答案比此处给出的更具建设性。
标签: python matplotlib histogram subplot