'how to plot a figure under a video?

I'm using the following code to create a plot of the brightness as a function of frame but it won't plot it on the same window. I'm very new a using python and not sure how to go around this issue. I appreciate any help.

import cv2
import numpy as np 
import matplotlib.pyplot as plt
import datetime 
import asilib

#Reads the video and collects it information
cap = cv2.VideoCapture('20150326_060700_062957_themis_rank_fisheye.mp4')
fps = cap.get(cv2.CAP_PROP_FPS)
width  = cap.get(cv2.CAP_PROP_FRAME_WIDTH)   # float
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)  # float
timestamps = [cap.get(cv2.CAP_PROP_POS_MSEC)]

fourcc = cv2.VideoWriter_fourcc(*'MP4V')
output = cv2.VideoWriter("output.mp4", fourcc, fps,(int(width),int(height)))
#plot
fig, ax = plt.subplots(2, 1, figsize=(7, 10))

while (cap.isOpened()):
   ret, frame1 = cap.read()
   if (ret):
     #get date time
     dt = datetime.datetime.now()
    # Adds the rectangles in all frames
     ROI = cv2.rectangle(frame1, (135, 510), (200,450), (255, 0, 0), 1)
     height, width, channels = ROI.shape 
     #Accessing BGR pixel values    
     for x in range(0, width) :
       for y in range(0, height) :
          blue = (ROI[x,y,0]) #B Channel Value
          green = (ROI[x,y,1]) #G Channel Value
          red = (ROI[x,y,2]) #R Channel Value
          brightness = (0.2126*red) + (0.7152*green) + (0.0722*blue)
      # Plot the time series of the mean ASI intensity along the satellite path
       ax[1].plot(frame1, brightness)
       ax[1].axvline(timestamps, c='b')  # At the current image time.
       
     output.write(frame1)
     Output = cv2.imshow("output", frame1)
     if cv2.waitKey(1) & 0xFF == ord('s'):
        break
   else:
         break

print(brightness)
cv2.destroyAllWindows()  
output.release()  
cap.release()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source