When researching sorting algorithms on the Internet, we often see animations that visualize the behavior of the algorithms. I thought it would be interesting if I could make such an animation myself, so I tried using matplotlib.
[Bubble Sort](https://ja.wikipedia.org/wiki/%E3%83%90%E3%83%96%E3%83%AB%E3%82%BD%E3%83%BC%E3% This is the code that outputs the behavior of 83% 88) to a gif animation.
import matplotlib.pyplot as plt
import matplotlib.animation as ani
from random import shuffle
COLOR_PINK = '#ffc0cb'
COLOR_GOSSIP = '#cbffc0'
def draw_bar(li, color=COLOR_PINK):
x_axis = list(range(1, len(li) + 1))
plt.bar(x_axis, li,
tick_label=li, align='center', width=0.4, color=color)
def draw_frame(frame, sorted_li, steps):
plt.clf()
li = steps[frame]
options = {'color': COLOR_GOSSIP} if li == sorted_li else {}
draw_bar(steps[frame], **options)
def bubble_sort(li):
sorted_li = list(li)
steps = []
for i in range(0, len(sorted_li)):
for j in range(1, len(sorted_li) - i):
if sorted_li[j - 1] > sorted_li[j]:
sorted_li[j], sorted_li[j - 1] = sorted_li[j - 1], sorted_li[j]
if len(steps) == 0 or steps[-1] != sorted_li:
steps.append(list(sorted_li))
return sorted_li, steps
if __name__ == '__main__':
li = list(range(1, 11))
shuffle(li)
sorted_li, steps = bubble_sort(li)
fig = plt.figure(figsize=(6.0, 6.0))
#Make the sort result appear longer by 5 frames in the animation.
# FuncAnimation()To repeat_There is a delay option, but this doesn't work.
steps = steps + [steps[-1]] * 5
anim = ani.FuncAnimation(fig, draw_frame,
fargs=(sorted_li, steps), frames=len(steps))
anim.save('bubble_sort.gif', writer='imagemagick', fps=5)
Recommended Posts