我想编写负责分类算法可视化和测试有效性的电子学习应用程序。我将使用Python和PySide(PyQt),但是使用任何漂亮的工具来实现这种简单的可视化效果会很棒。
该可视化的目的是显示工作排序算法的步骤。

您知道用Python制作简单的可视化动画的漂亮工具吗?
也许PySide可以进行可视化?

最佳答案

您可以使用matplotlib及其动画功能:

import matplotlib.pyplot as plt
import matplotlib.animation as animation

random_list = [10,3,5,4,9,1,6,7,2,8]

def bubble_sort():
    data = random_list
    for i in range(len(data)-1):
        for j in range(i, len(data)-1):
            a, b = data[j], data[j+1]
            if a > b:
                data[j], data[j+1] = b, a
                yield data

fig = plt.figure()
ax = fig.add_subplot(111)
def update(data):
    ax.clear()
    ax.hlines(range(len(data)), 0, data, 'red')
    ax.set_ylim(-0.5, 9.5)
update(random_list)

ani = animation.FuncAnimation(fig, update, bubble_sort, interval=250)
plt.show()


您只需要更改bubble_sort()即可实现其他算法,并生成一个数字列表,以便在每次迭代时进行绘制。

matplotlib也可以是embedded in PySide,但是我没有对其进行测试。

关于python - 使用Python进行可视化的漂亮工具,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13481378/

10-14 20:19
查看更多