Python Matplotlib:有没有办法在轮廓动画中制作动态图例?

时间:2021-07-12 11:12:37

标签: python matplotlib animation legend contourf

我正在模拟一个立方体的热传递。我想用轮廓图的 Animation.Artist 制作动画。我已经完成了这一步,但是,我想添加一个带有模拟当前时间的图例,例如“t = 0.1 s”,例如,在特定时间对应的每一帧更新。

我尝试了很多失败的事情,因为创建了一个 text = ['t = ' + str(iteration*dt) + 's'] 与 plt.legend(text) 结合并将其放入迭代循环中,但只是最后一次显示在动画上(见我的代码)。

此外,我在论坛上搜索了其他解决方案,但似乎没有什么好的...

有人可以帮我处理模拟动画的动态图例吗?提前致谢!

这是我的代码:

......
......
                         
X,Z = np.meshgrid(x,z)      
fig = plt.figure(1)
ims = [] 

# Iterative loop
for n in range(0,8):
    print(n)
   ....
        for i in range(0,N+2):
            for j in range(0,N+2):
                for k in range(0,N+2):    
                       ......
                       ......

    im = plt.contourf(X,Z,M.T,100,cmap='plasma')
    text = ['t = ' + str(n*dt) + ' s']
    plt.legend(text,loc='upper left')
    ims.append(im.collections)
    if n == 7:
        print("No more iteration.")



ani = animation.ArtistAnimation(fig, ims, interval = 1000, blit=True)
print("CPU = ",time.process_time()," s") # CPU time in seconds
plt.xlabel('x')
plt.ylabel('z')
plt.xlim(-0.05,1.05)
plt.ylim(-0.05,1.05)
plt.colorbar(label='Température en °K')
ani.save("Evolution_T_2D.mp4")
plt.show()

0 个答案:

没有答案
相关问题