gpt4 book ai didi

python - 如何在 matplotlib 中淡化动画 gif 的曲线

转载 作者:行者123 更新时间:2023-12-05 05:33:59 25 4
gpt4 key购买 nike

我有一个 gif,但我想淡化绘制的曲线。我读过this solution但我仍然需要帮助。目前所有曲线都相同:

enter image description here

这段代码为我制作了 gif:

import matplotlib
import imageio
def plot_for_offset(time, value):
ims = []
fig, axs = plt.subplots(figsize=(6,2.5))
for t, v in zip (time, value):
axs.plot(t, v, lw=1.0, color='k')
# Used to return the plot as an image rray
fig.canvas.draw() # draw the canvas, cache the renderer
image = np.frombuffer(fig.canvas.tostring_rgb(), dtype='uint8')
image = image.reshape(fig.canvas.get_width_height()[::-1] + (3,))
ims.append(image)
return ims
kwargs_write = {'fps':1.0, 'quantizer':'nq'}

import numpy as np
time = np.array([[0., 1., 2.], [0., 1., 2.], [0., 1., 2.]])
value = np.array([[0., 10., 20.], [20., 10., 0.], [10., 10., 10.]])
imageio.mimsave('animation.gif', plot_for_offset(time, value), fps=1.0)

现在,我想淡化过去弹出的曲线。我非常感谢任何帮助。

最佳答案

您可以通过在动画的每一帧更改绘图的透明度 alpha 来实现。

请参阅下面使用 matplotlib 中的 FuncAnimation 函数的示例(文档 here):

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

time = np.array([[0., 1., 2.], [0., 1., 2.], [0., 1., 2.],[0, 1, 2],[0, 1, 2]])
value =np.array([[0., 10., 20.], [20., 10., 0.], [10., 10., 10.],[2, 2 ,2],[7, 14, 18]])
fig, axs = plt.subplots(figsize=(6,2.5))
ln=[plt.plot(time[i], value[i], lw=2.0, color='k',alpha=i==0) for i in range(len(time))]
ln=[ln[i][0] for i in range(len(time))]
axs.set_xlim(0, 2)
axs.set_ylim(0, 20)
step=5

def update(frame):

if frame<=(len(ln)*step)-1: # generalize to N curves
ln[frame//step].set_alpha(1)
[ln[i].set_alpha(0) for i in range(frame//step+1, len(ln))]
[ln[i].set_alpha(1/(frame-(i+1)*step+1)) for i in range(frame//step)]
else:
[ln[i].set_alpha(1/(frame-(i+1)*step+1)) for i in range(len(ln)-1)]
return ln

ani = FuncAnimation(fig, update, frames=10*step,blit=True)

输出给出: enter image description here

关于python - 如何在 matplotlib 中淡化动画 gif 的曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73707628/

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