如何显示多个人物的动画

时间:2019-04-27 21:24:16

标签: python python-3.x matplotlib animation jupyter-notebook

我正在尝试解决任何图形的色度问题。我也想展示色彩变化的动画。我正在绘制每个状态的图。假设在此示例中(针对模拟),我更改了塔斯马尼亚州和西澳大利亚州的颜色。

import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import pylab
from matplotlib.pyplot import pause
import matplotlib._pylab_helpers
import matplotlib.animation as animation


australia ={}

australia['T'] = []
australia['WA'] = ['NT', 'SA']
australia['NT'] = ['WA', 'Q', 'SA' ]
australia['SA'] = ['WA', 'NT', 'Q', 'NSW', 'V']
australia['Q'] = ['NT', 'SA', 'NSW']
australia['NSW'] = ['Q', 'SA', 'V']
australia['V'] = ['SA', 'NSW']
G = nx.Graph(australia)
assignment = {}
fig = plt.figure()
#{'T': 'R', 'WA': 'R', 'NT': 'G', 'SA': 'B', 'Q': 'R', 'NSW': 'G', 'V': 'R'}
def get_col_fig(c):
    if c==1:
        assignment['T'] = 'R'
    if c==2:
        assignment['T'] = 'G'
    if c==3:
        assignment['WA'] = 'B'
    if c==4:
        assignment['WA'] = 'R'
    food_list=list(assignment.values())

    values = [assignment.get(node, 'K') for node in G.nodes()]
    fig = plt.figure()
    nx.draw(G, cmap=plt.get_cmap('viridis'), node_color=values, with_labels=True, font_color='white', alpha=1, linewidths=1, node_size=300)
    #pylab.plot()
    return fig


ims = []
for i in range(0,5):
    figrs = get_col_fig(i)

figures=[manager.canvas.figure
         for manager in matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]
print(figures)
for i, figure in enumerate(figures):
    figure.savefig('figure%d.png' % i)

我也检查了png图像是否显示颜色变化。 但我想将这些数字组合起来并显示为动画。所以,我的想法是:


ims.append([figrs])
ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,
                                repeat_delay=1000)
FFwriter = animation.FFMpegWriter()
ani.save('basic.mp4', writer = FFwriter)
plt.show()
os.system("ffmpeg -i basic.mp4 ani.gif")
from IPython.display import Image
Image(filename='ani.gif') 

但是它不起作用!

0 个答案:

没有答案