python - 在同一窗口但在不同的图中绘制两个直方图

标签 python numpy opencv

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

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()

最佳答案

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

您的代码应如下所示:

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()

关于python - 在同一窗口但在不同的图中绘制两个直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52736914/

相关文章:

Python:不工作 StatsModels

python - 将整个二进制文件读入 Python

python - numpy加载文件错误

python - 在没有循环的情况下将 numpy 数组中的 1's to 0 and 0' s 更改为 1

android - Gradle Android 构建系统 NDK 问题

algorithm - k-means++ vs 初始​​中心随机

python - tensorflow 安装正确,但在导入 tensorflow 为 tf 时出现错误 "ImportError: No module named ' _pywrap_tensorflow_internal'

python - 读取大 CSV 后跟 `.iloc` 切片列时出现 Pandas MemoryError

python - TeX 输入的引号字符串

android - 为什么 Unity 在链接到 OpenCV 时无法加载 android native 库?