1

I want to plot two different histograms in different plots but opening in same window. With the following code, I am getting two histogram in same plot. I could not get the histogram with subplot don't know where I went wrong.

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()
user162817
  • 65
  • 2
  • 7

1 Answers1

0

It sounds like what you want is to create two subplots. For this you should use the subplots function from matplotlib.

Your code should look something like this:

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()
Dominique Paul
  • 1,623
  • 2
  • 18
  • 31