I have been making a sorting visualizer in Python, based of "Sound of Sorting". I've ran into a problem. I can't run matplotlib plot and update it, while the sorting is happening, and I want the plot to be animated. The obvious solution would be asyncio or multi-threading, but I can't get it to work. I can't think up a solution to this and have resorted to asking for help. Here is the code:
from random import randint
from timeit import repeat
from matplotlib import animation
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
import time
import numpy as np
import os
import asyncio
def bubble_sort(array):
n = len(array)
global arrayGlobal
for i in range(n):
already_sorted = True
for j in range(n - i - 1):
if array[j] > array[j + 1]:
array[j], array[j + 1] = array[j + 1], array[j]
already_sorted = False
if already_sorted:
break
arrayGlobal = array
time.sleep(0.1)
print(arrayGlobal)
return array
def visTest():
reshaped = np.reshape(arrayGlobal, (5, 5))
fig = plt.figure()
def animate(i):
sns.heatmap(reshaped, cbar=False)
anim = animation.FuncAnimation(fig, animate)
plt.show()
ARRAY_LENGTH = 25
array = [randint(0, 100) for i in range(ARRAY_LENGTH)]
async def start():
loop = asyncio.get_running_loop()
await asyncio.gather(loop.run_in_executor(None, bubble_sort, (array)), loop.run_in_executor(None, visTest))
asyncio.run(start())
Thank you in advance.
