我有一个二维数组 (39, 57601),其中列代表时间(总共 57601 个时间点),行代表特定节点位置的温度(总共 39 个节点)。换句话说,数组中的每一列描述了相应时间点的温度曲线。我可以创建温度曲线的静态图,但我想可视化温度曲线如何随时间变化。我希望每行在绘图上单独显示,然后在 0.1(?) 秒后移至下一行,依此类推。
我在下面提供了一些基本代码来表示数据。
import numpy as np
import matplotlib.pylab as plt
#creating some dummy data (4 nodes, 5 timepoints) - values not important
T1 = np.array([1.1, 1.2, 1.3, 1.4, 1.5])
T2 = T1**2
T3 = T1**3
T4 = T1**4
T=np.stack((T1, T2, T3, T4))
position = np.array([0, 2, 4, 6])
#visualising all lines at once
plt.plot(position,T[:,0], label='0s', marker='o')
plt.plot(position,T[:,1], label='10s', marker='o')
plt.plot(position,T[:,2], label='20s', marker='o')
plt.plot(position,T[:,3], label='30s', marker='o')
plt.plot(position,T[:,4], label='40s', marker='o')
plt.legend()
plt.xlabel('Position (mm)')
plt.ylabel('Temperature (C)')
plt.show()
您可以使用 matplotlib.animation.FuncAnimation - 请参阅 https://matplotlib.org/stable/api/_as_gen/matplotlib.animation.FuncAnimation.html
import numpy as np
import matplotlib.pylab as plt
import matplotlib.animation as animation
#creating some dummy data (4 nodes, 5 timepoints) - values not important
T1 = np.array([1.1, 1.2, 1.3, 1.4, 1.5])
T2 = T1**2
T3 = T1**3
T4 = T1**4
T=np.stack((T1, T2, T3, T4))
position = np.array([0, 2, 4, 6])
fig = plt.figure()
ax = fig.add_subplot(111, )
a = ax.plot( position,T[:,0], label='0 s', marker='o' )
ax.set_xlabel('Position (mm)')
ax.set_ylabel('Temperature (C)')
ax.set_ylim( top=4 )
ax.legend( loc='upper left' )
def animate( i ): # update anything that has changed
a[0].set_label( f'{i*10} s' )
ax.legend( loc='upper left' )
a[0].set_data( position, T[:,i] )
ani = animation.FuncAnimation( fig, animate, interval=1000, frames=len( T ), repeat=False )
plt.show()
# ani.save( "demo.gif", fps=1 )