问题
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)
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
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.
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.
I'd like to know what I'm doing wrong in this case; how can I correct the unexpected behavior of num
?
回答1:
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 : 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)
来源:https://stackoverflow.com/questions/64743729/funcanimation-being-called-too-many-times