下面的示例代码创建了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()