如何在图像旁边绘制直方图?

时间:2017-07-08 07:12:47

标签: python plot

我想评估一个寻找文档“正确”轮换的想法。我们的想法是应用边缘检测器并计算每行和每列的白色值。为了评估它,我想在图像旁边绘制这个计数(或具有小容器尺寸的直方图)。我怎么能这样做?

我做了什么

#!/usr/bin/env python

import scipy.misc
import numpy as np
import scipy.ndimage
import seaborn as sns

im = scipy.misc.imread("morc-0102.jpg", mode="L")
height, width = im.shape
k = np.array([[1, -1],
              [1, -1]])
im = scipy.ndimage.convolve(im, k, mode='constant', cval=0.0)

# Calculate histogram
hist_height = np.zeros(height)
hist_width = np.zeros(width)
for y in range(height):
    for x in range(width):
        hist_height[y] += im[y][x]
        hist_width[x] += im[y][x]
sns.distplot(hist_height, kde=False, rug=False)
sns.distplot(hist_width, kde=False, rug=False)
sns.plt.show()
scipy.misc.imshow(im)

我想要什么

类似于此的可视化:在图像的右侧绘制(标准化的)计数hist_height,在底部绘制(标准化的)计数hist_width

enter image description here

我想我以前见过的东西非常相似。我知道如何通过调整画布大小并“手动”绘制每一行来手动完成此操作,但我想像matplotlib / seaborn这样的图书馆直接支持这个吗?

1 个答案:

答案 0 :(得分:0)

如何添加两个条形图作为子图?像这样,例如

import matplotlib.pyplot as plt
import numpy as np

# remove upper corner plot
ax1 = plt.subplot2grid((3, 3), (0, 2))
ax1.axis('off')

# plot some example image
imageExample = np.random.uniform(0,1,(5,5))
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2)
ax2.imshow(imageExample)
ax2.axis('off')

# use 'barh' for horizontal bar plot
horzSum = np.sum(imageExample,1)
ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2, sharey=ax2)
ax3.barh(range(len(horzSum)), horzSum)
ax3.axis('off')

vertSum = np.sum(imageExample, 0)
ax4 = plt.subplot2grid((3, 3), (0, 0), colspan=2, sharex=ax2)
ax4.bar(range(len(vertSum)), vertSum)
ax4.axis('off')

plt.subplots_adjust(wspace=0, hspace=0)
plt.show()

See here for the result

widthbar的{​​{1}}设置为1和barh,您会获得与您的目标非常相似的内容,除了您可能可以摆脱的空白不知何故。

enter image description here