下面的示例代码创建了2个情节,试图传达所需的动画想要做的事情.请注意,右侧的图与左侧的图相似,但表示时间经过t后波的位置.目标是动画波浪的向下运动,并使用 colored颜色 描绘波浪. 采用使用LineConnections的方法的原因是因为间隔不一定是规则的(请注意,z中的前两个值与z中的其余数据遵循相同的模式. 有没有方法可以修改下面的小可重复示例,以动画充满色彩的波浪的向下运动? 我还没有找到使用LineConnections的动画功能的示例. [Also,我对其他容纳不规则间隔数据的方法持开放态度(即,z位置可能不规则间隔)]

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

# x position of 1D profile
x = [0.5 for i in np.arange(20)]
# z position of 1D profile
z = [59.95, 59.70, 59.25, 58.75, 59.25,
     58.75, 58.25, 57.75, 57.25, 56.75,
     56.25, 55.75, 55.25, 54.75, 54.25,
     53.75, 53.25, 52.75, 52.25, 51.75]

points = np.array([x, z]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)

norm = plt.Normalize(
    0.0, 2.0
)

# data at time t1
t = 1
t1 = np.sin(2 * np.pi * (np.linspace(0,2,len(z)) - 0.01 * t)) + 1
# data as time t20
t = 20
t20 = np.sin(2 * np.pi * (np.linspace(0,2,len(z)) - 0.01 * t)) + 1

cmap = 'viridis'
lc1 = LineCollection(segments, cmap=cmap, norm=norm)
lc1.set_array(t1)
lc1.set_linewidth(50)

lc2 = LineCollection(segments, cmap=cmap, norm=norm)
lc2.set_array(t20)
lc2.set_linewidth(50)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(3,6), tight_layout=True)
ax1.add_collection(lc1)
line2 = ax2.add_collection(lc2)
plt.colorbar(
    line2, 
    shrink=1.0, 
    ax=ax, 
    label="Temperature", 
    location='right', 
    pad=0.05, 
    fraction=0.05, 
    aspect=7
)

ax1.set_xlim(0.4, 0.6)
ax1.set_ylim(51, 60.5)
ax2.set_xlim(0.4, 0.6)
ax2.set_ylim(51, 60.5)

plt.show()

推荐答案

您可以使用在传递给FuncAnimationanimate函数中找到的set_array方法:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib import animation

# x position of 1D profile
x = [0.5 for i in np.arange(20)]
# z position of 1D profile
z = [59.95, 59.70, 59.25, 58.75, 59.25,
     58.75, 58.25, 57.75, 57.25, 56.75,
     56.25, 55.75, 55.25, 54.75, 54.25,
     53.75, 53.25, 52.75, 52.25, 51.75]

points = np.array([x, z]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)

norm = plt.Normalize(
    0.0, 2.0
)

# data at time t1
t = 1
data = np.sin(2 * np.pi * (np.linspace(0,2,len(z)) - 0.01 * t)) + 1

cmap = 'viridis'
lc1 = LineCollection(segments, cmap=cmap, norm=norm, array=data, linewidth=50)

fig, ax = plt.subplots(figsize=(3,6), tight_layout=True)
ax.add_collection(lc1)
plt.colorbar(
    lc1, 
    shrink=1.0, 
    label="Temperature", 
    location='right', 
    pad=0.05, 
    fraction=0.05, 
    aspect=7
)

ax.set_xlim(0.4, 0.6)
ax.set_ylim(51, 60.5)

def animate(t):
    # data at time t
    data = np.sin(2 * np.pi * (np.linspace(0,2,len(z)) - 0.01 * t)) + 1    
    lc1.set_array(data)
    
    return lc1,

ani = animation.FuncAnimation(
    fig, animate, interval=20, blit=True, save_count=100)

ani.save('waves.gif')
plt.show()

enter image description here

Python相关问答推荐

如何使用scikit-learn Python库中的Agglomerative集群算法以及集群中声明的对象数量?

如何使用函数正确索引收件箱?

保留包含pandas pandras中文本的列

使用Python OpenCV的文本检测分割

如何将带有逗号分隔的数字的字符串解析为int Array?

在应用循环中间保存pandas DataFrame

Odoo -无法比较使用@api.depends设置计算字段的日期

TARete错误:类型对象任务没有属性模型'

将整组数组拆分为最小值与最大值之和的子数组

如果值不存在,列表理解返回列表

我如何使法国在 map 中完全透明的代码?

Pre—Commit MyPy无法禁用非错误消息

有没有一种ONE—LINER的方法给一个框架的每一行一个由整数和字符串组成的唯一id?

使用groupby方法移除公共子字符串

为什么numpy. vectorize调用vectorized函数的次数比vector中的元素要多?

如何更改groupby作用域以找到满足掩码条件的第一个值?

如何使regex代码只适用于空的目标单元格

重置PD帧中的值

pysnmp—lextudio使用next()和getCmd()生成器导致TypeError:tuple对象不是迭代器''

如何删除重复的文字翻拍?