Draw successive frames of a price chart with interpolated steps, turn each Matplotlib canvas into an image, and write and check an MP4 video with OpenCV.
After this lesson, you can turn a price chart into a short video that draws the price path step by step, as in the overview animation of the post on ETF index rebalancing.
How do I make the line move smoothly?
A video is a series of images called frames, and frames between the observed days make the line move smoothly, so np.linspace(0, 2, 9) gives nine drawing positions 0.25 apart and np.interp gives each position a price on the straight line between the observed closes. AAA’s made-up closes of 100, 104 and 102 dollars on days 0, 1 and 2 give 103.5 at position 1.25.
\[
104 + 0.25\,(102 - 104) = 103.5
\]
np.linspace(start, stop, number) includes both ends, and np.interp(positions, days, prices) needs the days in increasing order. Only frames 0, 4 and 8 show observed prices, and the six others are drawing aids that use the next day’s close, so they belong in the picture and never in a calculation.
The following box computes the nine positions and their prices.
How do I draw one frame and turn it into an image?
draw_frame(i) gives the line the first i + 1 positions with .set_data, shows the observed closes reached so far as dots, draws the canvas, and turns its pixels into an image for OpenCV. Frame 4, day 1, shows the closes of 100 and 104, and its image is 300 pixels high, 400 wide and 3 colour channels deep.
Figure(figsize=(4, 3), dpi=100) with FigureCanvasAgg draws into memory without opening a window, at 100 pixels per inch. The axis limits are set once, so every frame uses the same scale, and line, = ax.plot([], []) takes the single line out of the list that .plot returns. np.asarray(fig.canvas.buffer_rgba()) reads the pixels as red, green, blue and opacity, and cv2.cvtColor(..., cv2.COLOR_RGBA2BGR) reorders them into blue, green and red, the order OpenCV’s video writer expects. The package opencv-python provides the module cv2.
The following box prepares the chart, defines draw_frame and draws frame 4.
How do I write the frames into a video and check it?
cv2.VideoWriter(path, codec, fps, (width, height)) opens an MP4 file, .write(image) adds one frame, and .release() finishes the file, so nine frames at four frames per second make a video of 9 / 4 = 2.25 seconds. Reading the file back with cv2.VideoCapture and counting the frames that decode gives 9, the number written.
cv2.VideoWriter_fourcc(*"mp4v") names the MPEG-4 codec, the format that encodes the frames, with * splitting the text into its four letters. The image’s shape lists the height first, while the writer takes the width first. A writer that fails to open, because the codec is missing from the OpenCV build, raises an error, and finally releases the writer even if a frame fails. reader.read() returns whether a frame was read and the frame itself, and the video lands in the folder video_demo, emptied at the start.
The following box writes the video and counts the frames that decode.