Speeding up matplotlib animation which uses imshow

Viewed 49

I want to create a plot where there are 32x32 images tiled next to each other in a sort of mosaic shape. The images are (16, 16, 4) RGBA arrays. My current code to do it is so -

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

data = np.load("https://github.com/sharmaabhishekk/emoji-mosaic-mpl/blob/master/emoji_matches.npy")
tiled_left = np.tile(np.arange(0, 512, 16), 32)

def update(i):
    print(i)
    row = i//32
    ax.imshow(data[i], extent=[tiled_left[i], tiled_left[i] + 16, row*16, row*16 + 16])

fig, ax = plt.subplots()
ax.set(xlim=[0, 512], ylim=[0, 512], aspect=1)
ax.invert_yaxis()
ax.set_axis_off()

anim = animation.FuncAnimation(fig=fig, frames=1024, func=update, repeat=False, interval=50, cache_frame_data=False)

plt.show()

The logic looks fine enough to me but it's very slow. I'm not sure what optimization to use. I thought of using a zeros array and then masking in the data as call update. But that's even slower.

I'm running out of ideas. What could I try to speed up the process?

0 Answers
Related