FuncAnimation 被调用太多次 [英] FuncAnimation being called too many times

查看:95
本文介绍了FuncAnimation 被调用太多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是上一个我问过的问题的延续.我注意到用于更新饼图的值不正确.我在名为 update 的FuncAnimation函数中使用 num 迭代器更新了列表 z .这是我正在使用的代码:

This is a continuation of a previous question I asked. I noticed the values being used to update my pie chart were incorrect. I have a list z that is being updated with the num iterator in my FuncAnimation function named update. Here is the code I'm working with:

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

numbers = [[6.166, 5.976, 3.504, 7.104, 5.14],
 [7.472, 5.888, 3.264, 6.4825, 7.168],
 [7.5716, 9.936, 3.6, 8.536, 2.808],
 [2.604, 2.296, 0.0, 6.144, 4.836],
 [7.192, 4.932, 0.0, 6.016, 8.808],
 [7.192, 5.5755, 3.694, 9.376, 9.108],
 [7.63616, 5.912, 3.968, 6.672, 3.192],
 [3.41049, 5.44, 4.004, 7.212, 3.6954],
 [4.3143, 6.364, 3.584, 7.44, 5.78],
 [4.992, 3.9692, 4.272, 0.0, 2.528]]
numbers = np.array(numbers)

colors = ["yellow", "red", "purple", "blue", "green"]
explode = [0.01, 0.01, 0.01, 0.01, 0.01]
labels = ["DM", "Bard", "Warlock", "Paladin", "Ranger"]
z = np.array([0,0,0,0,0]).astype(np.float)

fig,ax = plt.subplots()
y = []
def update(num):
    global y
    global z
    ax.clear()
    ax.axis('equal')     
    z += numbers[num]
    y.append(z)
    
    #output of different vairables#
    print(num, z, sum(z), len(y))

    pie = ax.pie(z, explode=explode, labels=labels, colors=colors, 
                 autopct='%1.1f%%', shadow=True, startangle=140)
    ax.set_title(sum(z))    
    
ani = animation.FuncAnimation(fig, update, frames=range(10), repeat=False)
ani.save('test.gif', writer='pillow', fps=1)

print() 函数的输出如下所示:

The output of the print() function looks like this:

0 [6.166 5.976 3.504 7.104 5.14 ] 27.89 1
0 [12.332 11.952  7.008 14.208 10.28 ] 55.78 2
1 [19.804  17.84   10.272  20.6905 17.448 ] 86.05450000000002 3
2 [27.3756 27.776  13.872  29.2265 20.256 ] 118.5061 4
3 [29.9796 30.072  13.872  35.3705 25.092 ] 134.3861 5
4 [37.1716 35.004  13.872  41.3865 33.9   ] 161.3341 6
5 [44.3636 40.5795 17.566  50.7625 43.008 ] 196.27959999999996 7
6 [51.99976 46.4915  21.534   57.4345  46.2    ] 223.65975999999995 8
7 [55.41025 51.9315  25.538   64.6465  49.8954 ] 247.42165 9
8 [59.72455 58.2955  29.122   72.0865  55.6754 ] 274.90395 10
9 [64.71655 62.2647  33.394   72.0865  58.2034 ] 290.66515 11
0 [70.88255 68.2407  36.898   79.1905  63.3434 ] 318.55514999999997 12

打印输出显示 numbers[0] 被添加到自身 before num 迭代器增加 1.之后,它按预期工作,其中 numbers[1]numbers[9] 加在一起.但是同样,出于某些原因, numbers [0] 被添加到 numbers [9] 中.

The print output shows the that numbers[0] is being added to itself before the num iterator increases by 1. After that, it works as intended where numbers[1] through numbers[9] are added together. But again, numbers[0] is added to numbers[9] for some reason.

生成的gif的第一帧显示以下数据: [12.332 11.952 7.008 14.208 10.28] 55.78 最后一帧显示此数据: [64.71655 62.2647 33.394 72.0865 58.2034 ] 290.66515 这没问题,因为这是循环应该停止的地方.

The resulting gif's first frame displays this data: [12.332 11.952 7.008 14.208 10.28 ] 55.78 and the last frame displays this data: [64.71655 62.2647 33.394 72.0865 58.2034 ] 290.66515 which is okay since this is where the loop should stop.

我想知道在这种情况下我做错了什么;如何纠正 num 的意外行为?

I'd like to know what I'm doing wrong in this case; how can I correct the unexpected behavior of num?

推荐答案

如果没有将 init_func = 传递给 FuncAnimation ,它将使用动画函数本身作为初始状态,这会导致您的双重0.文档说明:

Without an init_func= passed to FuncAnimation, it will use the animation function itself as the initial state, which causes your doublet 0. The documentation states:

init_func:可调用,可选

init_func : callable, optional

用于绘制清晰边框的功能.如果没有给出,结果将使用帧序列中第一项的绘图.该函数将在第一帧之前被调用一次.

A function used to draw a clear frame. If not given, the results of drawing from the first item in the frames sequence will be used. This function will be called once before the first frame.

你可以简单地传递一个空函数来解决这个问题.

You can simply pass an empty function to fix the problem.

def init():
    pass

(...)

ani = animation.FuncAnimation(fig, update, frames=range(10), init_func=init, repeat=False)

这篇关于FuncAnimation 被调用太多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆