The following is a snippet of my code for a sorting visualiser:
fig, ax = plt.subplots(figsize = (15, 7))
a_list = [i for i in range(N, 0, -1)]
bar_rects = ax.bar(range(len(a_list)), a_list, align = 'edge')
ax.set_xlim(0, N)
ax.set_ylim(0, int(1.1 * N))
def update_fig(a_list, rects):
for rect, val in zip(rects, a_list):
rect.set_height(val)
anim = animation.FuncAnimation(fig,
func = update_fig,
fargs = (bar_rects,),
frames = generator,
interval = 1,
repeat = False)
plt.show()
The algorithm works and its performance is acceptable for about 150 bars. Problem arises when the number of bars is 200 onwards - the speed of the animation drops massively. I have a feeling that updating bar heights using a for loop could be inefficient.
Is there something wrong I’m doing or something inefficient in the code? Any way to improve the animation speed?