Python 改变pyplot'中的直方图;s子地块

Python 改变pyplot'中的直方图;s子地块,python,matplotlib,histogram,subplot,Python,Matplotlib,Histogram,Subplot,我正在比较python中图像的直方图。这就是我注意到同一幅图像的直方图,使用pyplot的子图绘制两次,根据子图索引的不同,直方图可能会有所不同。这里有一个简单的例子: 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, bi

我正在比较python中图像的直方图。这就是我注意到同一幅图像的直方图,使用pyplot的子图绘制两次,根据子图索引的不同,直方图可能会有所不同。这里有一个简单的例子:

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笔记本中的两个直方图,它们看起来就不同了:

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)
这怎么会发生?这是一张显示不同直方图的图像。它们甚至没有镜像

现在作为一个答案:

这似乎是matplotlib的一个错误,因为在两次打印相同数据时也会出现此问题。我在Github上打开了一个问题


解决方法是使用
plt.hist(img1,bins=255)
等等。

了解您的数据(即
blocks_1
)的外观会很有帮助,因为我无法用随机数据重现错误。问题似乎在于
plt.bar()
。如果您尝试两次绘制相同的直方图(即
ax1[0]。bar(bins3[:-1],hist3)ax1[1]。bar(bins3[:-1],hist3)
。这似乎是pyplot的一个缺陷?!作为一种解决方法,您可以简单地使用
plt.hist
而不是
np.histogram
plt.bar
我已经打开了一个;):P该死,太慢了。顺便说一句,如果这真的是一个bug,请给出答案。我会接受的。我标记为类似问题的副本,这些问题的答案比这里给出的更具建设性。