def visualize(self, num=100, save=False):
"""
Visualizes given Sorting Algorithm
:param num: Number of points that has to be chosen for visualization
:param save: Boolean indicating whether to save animation in 'output' directory
"""
plt.title(self.sorter.name + " Visualization")
plt.xlabel("Array Index")
plt.ylabel("Element")
data = np.arange(num)
ran.shuffle(data)
self.sorter.sort(data, visualization=True)
self.hist_arr = self.sorter.hist_array
self.scatter = plt.scatter(np.arange(self.hist_arr.shape[1]), self.hist_arr[0]) # plt.scatter(x-array,y-array)
self.animation = animation.FuncAnimation(self.fig, self.__update, frames=self.hist_arr.shape[0], repeat=False,
blit=False, interval=1)
if save:
import os
import errno
path = "output"
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
path = os.path.join('output', self.sorter.name + ".mp4")
p1 = Process(
target=lambda: self.animation.save(path, writer=animation.FFMpegWriter(fps=100)))
p1.start()
plt.show()
评论列表
文章目录