2

I'm writing a short script in Python that is supposed to graph a series of scatter Cartesian points, one at a time as an animation (i.e., one frame is a point, the next frame is another). I'm using matplotlib.

My program currently plots correctly, but it returns a static plot with all the points on the plot, so something must be wrong with the animation function itself.

When I run the code now, despite returning a plot I receive AttributeError: 'PathCollection' object has no attribute 'set_data'. I've tried tweaking several small things, but generally that error message remains the same. Thank you in advance!

import matplotlib
from matplotlib import pyplot as plt
from matplotlib import animation 
import numpy as np

pole_list = [(1,2), (2,3), (3,3), (3,2), (3,1), (4,1), (3,1)] #arbitrary list of points

fig, ax = plt.subplots(1,1)
x_val = [x[0] for x in pole_list]
y_val = [x[1] for x in pole_list]
scatterplot = ax.scatter(x_val, y_val)
print(x_val, y_val)

def init():
    scatterplot.set_data([], [])
    return scatterplot,

def animate(i, x_val, y_val):
    scatterplot.set_data(x_val, y_val)
    return scatterplot,


animate = animation.FuncAnimation(fig, animate, fargs=(x_val,y_val), init_func=init, frames=8, interval=20, blit = True)
plt.show()
cats-cat
  • 21
  • 1
  • Python error messages are *helpful*. In this case it tells you that there is no `set_data` method for scatter plots. So the next step would probably be to google for something like "matplotlib animate scatter", which will directly lead you to e.g. [this question](https://stackoverflow.com/questions/9401658/how-to-animate-a-scatter-plot) for which I marked this as duplicate now. – ImportanceOfBeingErnest Mar 01 '19 at 11:10

0 Answers0