I'm plotting some data as scatter plots which is overlaid on an image. I would like to make an animation of this by plotting one scatter point at a time. This is the code I have right now using and editing the answer from here:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
x = random.sample(range(0, 287), 20)
y = random.sample(range(0, 380), 20)
size = [20 for x in range(20)]
colors = ["r" for x in range(20)]
cm = plt.get_cmap('jet')
fig = plt.figure(figsize=(18,9))
graph = plt.scatter([], [],marker='+')
url = 'https://raw.githubusercontent.com/kornelski/pngquant/master/test/img/test.png'
im = plt.imread(url)
def animate(i):
implot = plt.imshow(im)
graph.set_offsets(np.vstack((x[:i+1], y[:i+1])).T)
graph.set_sizes(size[:i])
graph.set_facecolors(colors[:i+1])
return graph
ani = FuncAnimation(fig, animate, repeat=False, interval=0.1)
plt.show()
There are two things I would like help with.
- I would like the color of my scatterplot to change based on a third variable, i.e use a
cmap
. However, the set_facecolors does not accept such an argument. - When I try to save my animation using
ani.save('files/animation.gif',writer='imagemagick', fps=60)
my jupyter notebook crashes.