我有一个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)

现在,我想要淡化过go 出现的曲线.我非常感谢你们的帮助.

推荐答案

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

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

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)

And the output gives: enter image description here

Python相关问答推荐

在for循环中保存和删除收件箱

如何在Power Query中按名称和时间总和进行分组

在Python中使用一行try

如何从格式为note:{neighbor:weight}的字典中构建networkx图?

Python panda拆分列保持连续多行

基本链合同的地址是如何计算的?

强制venv在bin而不是收件箱文件夹中创建虚拟环境

Python中的负前瞻性regex遇到麻烦

Python中MongoDB的BSON时间戳

我对我应该做什么以及我如何做感到困惑'

如何在Polars中从列表中的所有 struct 中 Select 字段?

使用Python更新字典中的值

迭代嵌套字典的值

导入...从...混乱

在pandas中使用group_by,但有条件

不能使用Gekko方程'

与命令行相比,相同的Python代码在Companyter Notebook中运行速度慢20倍

如何在Python中使用另一个数据框更改列值(列表)

matplotlib + python foor loop

跳过嵌套JSON中的级别并转换为Pandas Rame