Borol
Borol

Reputation: 247

AttributeError: 'numpy.int32' object has no attribute 'get_zorder' when using FuncAnimation in matplotlib

I'm getting the error only when I set Blit=True in my code below, it works when I set Blit=False. Unfortunately, I need to have Blit=True because it makes the animation much smoother (at least that's what I suppose). What's wrong here? I saw other threads with the same error message, but it wasn't specifially for stuff including matplotlib.

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

fig, ax = plt.subplots()

ax.axis('off')

circle_red1 = plt.Circle((1, 1), 1, color='red')
circle_red2 = plt.Circle((0, 0), 1, color='red')
circle_red3 = plt.Circle((0, 1), 1, color='red')
circle_red4 = plt.Circle((1,0), 1, color='red')

ax.add_artist(circle_red1)
ax.add_artist(circle_red2)
ax.add_artist(circle_red3)
ax.add_artist(circle_red4)

circle_white = plt.Circle((0.5, 0.5), 0.1, color='white')

ax.add_artist(circle_white)

fig.set_size_inches(5, 5)

def update(i, fig, ax):
    while i <= 50:
        circle_white = plt.Circle((0.5, 0.5), i/100, color='white')
        ax.add_artist(circle_white)

        return fig, ax, i

    while 50 < i <= 100:
        circle_red1 = plt.Circle((1, 1), i/1000, color='red')
        circle_red2 = plt.Circle((0, 0), i/1000, color='red')
        circle_red3 = plt.Circle((0, 1), i/1000, color='red')
        circle_red4 = plt.Circle((1, 0), i/1000, color='red')

        ax.add_artist(circle_red1)
        ax.add_artist(circle_red2)
        ax.add_artist(circle_red3)
        ax.add_artist(circle_red4)

        return fig, ax, i

anim = FuncAnimation(fig, update, frames=np.arange(0, 100, 1), interval=10, blit=True, repeat=False, fargs=(fig, ax))

plt.show()

Upvotes: 1

Views: 352

Answers (1)

Stef
Stef

Reputation: 30589

If blit is True your update function must return an iterable of all artists that were modified or created, see documentation:

def update(i, fig, ax):
    while i <= 50:
        circle_white = plt.Circle((0.5, 0.5), i/100, color='white')
        ax.add_artist(circle_white)

        return [circle_white]

    while 50 < i <= 100:
        circle_red1 = plt.Circle((1, 1), i/1000, color='red')
        circle_red2 = plt.Circle((0, 0), i/1000, color='red')
        circle_red3 = plt.Circle((0, 1), i/1000, color='red')
        circle_red4 = plt.Circle((1, 0), i/1000, color='red')

        ax.add_artist(circle_red1)
        ax.add_artist(circle_red2)
        ax.add_artist(circle_red3)
        ax.add_artist(circle_red4)

        return [circle_red1, circle_red2, circle_red3, circle_red4]

Upvotes: 1

Related Questions