I have a task that calls a plot numerous times and creates a gif from the results (see below for a reproducible example).
My question is: I would like to add a background image to this gif. My guess is, I would have to add it somewhere in the loop, before appending.
What I found so far, although hilarious did not really relate to my question:
Plotting a cropped background image on a matplotlib graph
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.animation import PillowWriter
import time
rng = np.random.default_rng()
fig = plt.figure(figsize=[10, 9])
ims = []
for i in range(200):
df = pd.DataFrame(rng.integers(0, 100, size=(100, 2)), columns=list('xy'))
x = df["x"]
y = df["y"]
im = plt.plot(x, y, "b.")
ims.append(im)
print(i)
ani = animation.ArtistAnimation(fig, ims, interval=500, blit=True,
repeat_delay=1000)
writer = PillowWriter(fps=2)
ani.save("demo2.gif", writer=writer)
sources: