how to animate a scatterplot of pandas data with matplotlib

Viewed 2264

I'm currently trying to animate a scatterplot of monthly data saved in a pandas dataframe. So far I made loop, which generates one single plot after another. Now I would like to join them in a single gif (or mp4 I don't care). Is there an easy way to make use of mathplotlibs animation function? I can't get my head around, how to loop sliced data through FuncAnimation. So far I did this:

time = df.monat.unique()
for i in time:
    dft = df[(df.monat == i) & (df.xcol < 4000)]
    plt.scatter(x=dft['xcol'],
            y=dft['ycol'],
            s=dft['scol'] / 25,
            c=dft['clr'],
            linewidth=0,
            alpha=0.8)
    plt.title('Title ' + str(i), fontsize=10)
    plt.xlabel('x label', fontsize=9)
    plt.ylabel('y label', fontsize=9)
    legend1_line2d = list()
    for val in clrdict.values():
        legend1_line2d.append(mlines.Line2D([0], [0],
            linestyle='none',
            marker='o',
            alpha=0.6,
            markersize=6,
            markeredgecolor=None,
            markeredgewidth=0,
            markerfacecolor=val))
    legend1 = plt.legend(legend1_line2d,
             names,
             frameon=False,
             numpoints=1,
             fontsize=8,
             loc='upper right') 
    plt.show()
1 Answers
Related