【问题标题】:Plot two histogram in same window but in different plots在同一窗口但在不同的图中绘制两个直方图
【发布时间】:2018-10-10 09:27:07
【问题描述】:

我想在不同的图中绘制两个不同的直方图,但在同一个窗口中打开。使用以下代码,我在同一个图中得到两个直方图。我无法获得带有子图的直方图,不知道我哪里出错了。

from matplotlib import pyplot as plt

    img = cv2.imread(f)
    img1 = cv2.imread('compressed_' + f)
    color = ('b', 'g', 'r')
    for i, col in enumerate(color):
        histr = cv2.calcHist([img], [i], None, [256], [0, 256])
        hist = cv2.calcHist([img1], [i], None, [256], [0, 256])
        plt.plot(histr, color=col)
        plt.plot(hist, color=col)
        plt.xlim([0, 256])
        plt.title('Original image')
    plt.show()

【问题讨论】:

标签: python numpy opencv


【解决方案1】:

听起来您想要创建两个子图。为此,您应该使用 matplotlib 中的 subplots 函数。

您的代码应如下所示:

from matplotlib import pyplot as plt
import cv2

img = cv2.imread(f)
img1 = cv2.imread('compressed_' + f)
color = ('b', 'g', 'r')

fig, ax = plt.subplots(2,1)

for i, col in enumerate(color):
    histr = cv2.calcHist([img], [i], None, [256], [0, 256])
    hist = cv2.calcHist([img1], [i], None, [256], [0, 256])
    ax[0].plot(histr, color=col)
    ax[1].plot(hist, color=col)
    plt.xlim([0, 256])
    plt.title('Original image')
plt.show()

【讨论】:

  • ax[0] 和 ax[1] 取哪个值?他们没有被引用
  • @user162817 感谢您指出这一点。添加了缺少的行:)
猜你喜欢
  • 2020-02-23
  • 2015-01-10
  • 1970-01-01
  • 2015-01-24
  • 2015-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多