import matplotlib.pyplot as plt
import numpy as np
# Make a random plot...
fig = plt.figure()
fig.add_subplot(111)
# If we haven't already shown or saved the plot, then we need to
# draw the figure first...
fig.canvas.draw()
# Now we can save it to a numpy array.
data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
from moviepy.video.io.bindings import mplfig_to_npimage
import matplotlib.pyplot as plt
fig = plt.figure() # make a figure
numpy_fig = mplfig_to_npimage(fig) # convert it to a numpy array