【问题标题】:Changing histograms in pyplot's subplots [duplicate]更改 pyplot 子图中的直方图 [重复]
【发布时间】: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


【解决方案1】:

现在作为答案:

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

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

【讨论】:

    猜你喜欢
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    • 2017-08-31
    • 2021-01-25
    • 1970-01-01
    • 2013-12-24
    相关资源
    最近更新 更多