After many attempts I'm at a loss for how the func parameter works for animating bar charts. I have the below code:
# Imports
import random
from tkinter import *
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import pandas as pd
# Functions
def gen_rand_array(no=500):
return_list = []
for x in range(no):
return_list.append(random.randint(1,101))
return return_list
def bubblesort(list):
for i in range(len(list)):
for j in range(0,len(list) - i - 1):
if list[j] > list[j+1]:
list[j],list[j+1] = list[j+1],list[j]
yield list
# Runtime
def main():
# User parameters
win = Tk()
win.title("Set Parameters")
win.minsize(500,500)
array_size = Scale(win,from_=2,to=5000,orient="horizontal",label="Use the slider to set the size of the initial array:",length=450,sliderlength=10).pack()
win.mainloop()
# Build random unsorted list
unsorted_vals = []
for index,value in enumerate(gen_rand_array()):
unsorted_vals.append(value)
# Formatting
fig,ax = plt.subplots(figsize=(15,8))
def animate(x):
ax.set_title("Test")
ax.set_ylim(0,100)
for key,spine in ax.spines.items():
spine.set_visible(False)
ax.tick_params(bottom="off",left="off",right="off",top="off")
ax.set_yticks([])
ax.set_xticks([])
ax.bar(range(len(unsorted_vals)), unsorted_vals)
# Visualise the sort
sorter = bubblesort(unsorted_vals)
anim = animation.FuncAnimation(fig,frames=sorter,func=animate)
plt.show()
main()
So for each iteration of bubblesort() I want to animate the change in order on the bar chart. I've defined frames as my generator but I do not understand what I need to pass to the "func" parameter. I have been looking up examples online but I still don't understand what I need to put here. Whenever I run the code I get errors which do not explain the problem.