gpt4 book ai didi

python - 动画 matplotlib sankey 图

转载 作者:太空宇宙 更新时间:2023-11-04 01:01:40 24 4
gpt4 key购买 nike

我正在尝试为流量大小发生变化的 matplotlib Sankey 图制作动画。我有一个部分工作的示例,但是它不断添加计数器指示的更多 Sankey 图; len(sankey.diagrams)。这意味着它不会正常工作,有多个箭头,或者很长时间。如何正确地为带有多个箭头的 matplotlib Sankey 图制作动画?

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

time = np.linspace(0, np.pi * 2, 100)
flow_in = 4 * np.ones_like(time)
flow_out_a = 2 + np.sin(time)
flow_out_b = flow_in - flow_out_a

fig = plt.figure()
ax = fig.add_subplot(111)
sankey = Sankey(ax=ax, scale=1 / max(flow_in), format=u'%0.3g')
diags_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)

def init():
diags_text.set_text('')
sankey.diagrams=[]
a = flow_in[0]
b = -flow_out_a[0]
c = -flow_out_b[0]
sankey.add(flows=[a, b, c],
orientations=[0, 0, -1],
alpha=0.2, lw=2.0, trunklength=1, pathlengths=[1, 1, 1])
diag = sankey.finish()
return(diag, diags_text)

def anim(idx):
for diag in sankey.diagrams:
diag.patch.set_visible(False)
diag.text.set_visible(False)
for txt in diag.texts:
txt.set_visible(False)
a = flow_in[idx]
b = -flow_out_a[idx]
c = -flow_out_b[idx]
sankey.add(flows=[a, b, c],
orientations=[0, 0, -1],
alpha=0.2, lw=2.0, trunklength=1, pathlengths=[1, 1, 1])

diag = sankey.finish()
diags_text.set_text('len(sankey.diagrams) = {l}'.format(l=len(sankey.diagrams)))
return(diag, diags_text)

frames, = time.shape

anim = animation.FuncAnimation(fig, anim, init_func=init,
frames=frames, interval=20, blit=False)

最佳答案

可以在动画函数中添加行 sankey.diagrams = []

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

N = 10
time = np.linspace(0, np.pi * 2, N)
flow_in = 4 * np.ones_like(time)
flow_out_a = 2 + np.sin(time)
flow_out_b = flow_in - flow_out_a

fig, ax = plt.subplots()
sk = sankey.Sankey(ax=ax, scale=1 / max(flow_in), format=u'%0.3g')
diags_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)

def init():
a = flow_in[0]
b = -flow_out_a[0]
c = -flow_out_b[0]
sk.add(flows=[a, b, c],
orientations=[0, 0, -1],
alpha=0.2, lw=2.0, trunklength=1, pathlengths=[1, 1, 1])
diags = sk.finish()
return []

def anim(idx):
for diag in sk.diagrams:
# remove called instead of set_visible(False) to keep
# `len(ax.get_children())` constant
diag.patch.remove()
diag.text.remove()
for txt in diag.texts:
txt.remove()
sk.diagrams = []

a = flow_in[idx]
b = -flow_out_a[idx]
c = -flow_out_b[idx]

sk.add(flows=[a, b, c],
orientations=[0, 0, -1],
alpha=0.2, lw=2.0, trunklength=1, pathlengths=[1, 1, 1])
diags = sk.finish()
diags_text.set_text('len(sk.diagrams) = {l} | len(ax.get_children()) = {gc}'
.format(l=len(sk.diagrams), gc=len(ax.get_children())))
ax.set_xlim(-2, 3)
ax.set_ylim(-3, 2)
fig.savefig('frame_{:03d}.png'.format(idx))
return []

ax.set_xlim(-2, 3)
ax.set_ylim(-3, 2)

anim = animation.FuncAnimation(fig, anim, init_func=init, repeat=False,
frames=N, interval=20, blit=True)

# anim.save('mymovie.mp4', writer='mencoder')
plt.show()
subprocess.call(['convert', '-delay', '10', '-loop', '0', '*.png', 'out.gif'])

enter image description here

关于python - 动画 matplotlib sankey 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32555743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com