首页 > 解决方案 > 如何为 matplotlib FuncAnimation 定义 func 参数

问题描述

经过多次尝试,我对 func 参数如何为条形图设置动画感到茫然。我有以下代码:

# 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()

因此,对于 bubblesort() 的每次迭代,我想在条形图上按顺序对更改进行动画处理。我已经将框架定义为我的生成器,但我不明白我需要传递给“func”参数的内容。我一直在网上查找示例,但我仍然不明白我需要在这里放什么。每当我运行代码时,我都会收到无法解释问题的错误。

标签: pythonmatplotlib

解决方案


在内部animate(),您需要获取部分排序列表的下一个状态,并且需要相应地更新条形图。

您可以通过迭代获得更新的列表sorter,但取决于for loops您放置yield 语句的嵌套层,您的更新或多或少频繁。目前,您只产生一次排序列表。我将它向下移动了一个循环,但您可以进行更频繁的更新:

绘图的更新是通过h_bar(另请参阅此问题以了解一般更新条形图)

我留下了部分代码以专注于主要内容。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

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  # two elements swapped place
        yield list  # ith bubble loop completed 
    # yield list  # sorted list

n = 100
unsorted_vals = np.random.randint(0, 101, size=n)
sorter = bubblesort(unsorted_vals)

fig, ax = plt.subplots(figsize=(15, 8))
ax.set_ylim(0, 100)
for key, spine in ax.spines.items():
    spine.set_visible(False)
ax.set_yticks([])
ax.set_xticks([])

h_bar = ax.bar(range(len(unsorted_vals)), unsorted_vals)

def animate(x):
    current_list = sorter.__next__()
    for rect, h in zip(h_bar, current_list):
        rect.set_height(h)

anim = animation.FuncAnimation(fig, frames=n, func=animate, repeat=False)
plt.show()

现在你总是更新整个条形图,当只需要更新交换位置的两个元素时,也许你可以通过以下方式加速动画:

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, j

def animate(x):
    current_list, idx = sorter.__next__()
    h_bar[idx].set_height(current_list[idx])
    h_bar[idx+1].set_height(current_list[idx+1])
    return h_bar

推荐阅读