使用 LineConnection 制作一维数据动画

问题描述 投票:0回答:1

下面的示例代码创建了 2 个图,试图传达所需动画想要执行的操作。请注意,右侧的图与左侧的图类似,但代表了时间流逝后波的位置

t
。目标是制作波浪向下运动的动画,并使用颜色描绘波浪。采用使用
LineConnections
的方法的原因是因为间距不一定是规则的(请注意,
z
中的前两个值与
z
中的其余数据不遵循相同的模式。是否存在一种修改下面的小可重复示例以动画填充颜色的波浪的向下运动的方法?我还没有找到使用
LineConnections
的动画功能的示例[此外,我对其他可以适应的方法持开放态度。不规则间隔的数据(即
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()
matplotlib matplotlib-animation
1个回答
0
投票

您可以使用已在传递给

set_array
animate
函数中找到的
FunAnimation
方法:

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

© www.soinside.com 2019 - 2024. All rights reserved.