0

I am trying to write a program that will run a persistent homology animation on some random data. Here is the code that I have so far:

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

np.random.seed(0)
data = np.random.rand(50, 2)

diagrams = ripser(data)['dgms']

fig, ax = plt.subplots()
scat = ax.scatter(data[:, 0], data[:, 1], c='black')
bars = [ax.bar([], [], color='blue', alpha=0.5) for i in range(len(diagrams))]

def update(frame):
    for i, bar in enumerate(bars):
        ax.collections.clear()
        scat = ax.scatter(data[:, 0], data[:, 1], c='black')
        bar.clear()
        bar_data = diagrams[i]
        bar_data = sorted(bar_data, key=lambda x: x[1] - x[0], reverse=True)
        for j, (birth, death) in enumerate(bar_data):
            bar.add_patch(plt.Rectangle((j + 0.1, birth), 0.8, death - birth, alpha=0.5, color='blue'))
        ax.set_title(f"Frame {frame}, Homology Dimension {i}")
    return scat, bars

ani = animation.FuncAnimation(fig, update, frames=range(len(diagrams[0])), blit=True)
plt.show()

When I try to run this I get the errors:

AttributeError: 'FuncAnimation' object has no attribute '_resize_id'

AttributeError: 'ArtistList' object has no attribute 'clear'

any ideas as to what might be wrong here?

1 Answers1

0

It's a matplotlib version issue. I am assuming that you are working under matplotlib==3.7.x. There has been a technical change made to matplotlib so that .clear can no longer work here.

Try to downgrade to matplotlib==3.6.3, and this will hopefully resolve this issue.