在 pyplot 的子图中更改直方图
Changing histograms in pyplot's subplots
我正在比较 python 中图像的直方图。那是当我注意到同一图像的直方图,使用 pyplot 的 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 笔记本中的两个直方图看起来会有所不同:
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 here.
上开了一个问题
解决方法是使用 plt.hist(img1, bins=255)
等。
我正在比较 python 中图像的直方图。那是当我注意到同一图像的直方图,使用 pyplot 的 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 笔记本中的两个直方图看起来会有所不同:
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 here.
上开了一个问题解决方法是使用 plt.hist(img1, bins=255)
等。