使用 FuncAnimation 动画饼图仅显示最后一帧 [英] Animating pie chart using FuncAnimation showing last frame only

查看:49
本文介绍了使用 FuncAnimation 动画饼图仅显示最后一帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Matplotlib的FuncAnimation函数更新饼图并制作gif.我问过

基于此输出,仅将总计显示在最后一帧中.没有中间动画正在进行.我已经阅读了

I'm trying to use Matplotlib's FuncAnimation function to update a pie chart and make a gif. I've asked this question and it didn't get much traction. So Here is my dataset, a list of pandas series which I'll refer to as numbers, shown simplified below:

[[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]]

Here is the code I'm working with:

colors = ["yellow", "red", "purple", "blue", "green"]
explode = [0.01, 0.01, 0.01, 0.01, 0.01]
labels = ["DM", "Bard", "Warlock", "Paladin", "Ranger"]
z = [0,0,0,0,0]

fig,ax = plt.subplots()

def update(num):
    
    ax.axis('equal')
    global z
    for x in range(0,10):
        z += numbers[x]
        z = z.tolist()
        
        
        
    ax.pie(z, explode=explode, labels=labels, colors=colors, 
            autopct='%1.1f%%', shadow=True, startangle=140)

    ax.set_title(sum(z))    
    
ani = FuncAnimation(fig, update, frames=range(10), repeat=False)
plt.show()

The output:

Based on this output only the totals are being displayed in the final frame. There is no intermediary animation going on. I've read this question and answer but I cannot figure out why this isn't working.

解决方案

You need to clear the ax at the beginning of update, besides I think this part

for x in range(0,10):
    z += numbers[x]
    z = z.tolist()

is incorrect. Try the code below

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

def update(num):
    
    global z
    
    ax.clear()
    ax.axis('equal')     
    z += numbers[num]  
    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 is

这篇关于使用 FuncAnimation 动画饼图仅显示最后一帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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