import scipy.io
import matplotlib.pyplot as plt
import imageio
import numpy as npdata = scipy.io.loadmat("/文件路径/Sol.mat")# 提取数据
t_current = data['t'].flatten()
XX, YY = np.meshgrid(data['x'].flatten(), data['y'].flatten())
u_pred_plot_final = data['u_pred_plot_final']
v_pred_plot_final = data['v_pred_plot_final']
p_pred_plot_final = data['p_pred_plot_final']# 创建保存GIF的函数
def save_gif(images, filename):imageio.mimsave(filename, images, fps=5)# 初始化存储每个子图的图像列表
uvp_pred_images = []for k in range(len(t_current)):fig = plt.figure()plt.subplots_adjust(wspace=1, hspace=1)fig.suptitle('t = ' + str(t_current[k]), fontsize=15)plt.subplot(3, 1, 1)plt.pcolor(XX, YY, u_pred_plot_final[:, :, k], cmap='viridis')plt.axis('equal')plt.colorbar()plt.title('u_pred')plt.show()plt.subplot(3, 1, 2)plt.pcolor(XX, YY, v_pred_plot_final[:, :, k], cmap='Blues')plt.axis('equal')plt.colorbar()plt.title('v_pred')plt.subplot(3, 1, 3)plt.pcolor(XX, YY, p_pred_plot_final[:, :, k], cmap='cividis')plt.axis('equal')plt.colorbar()plt.title('p_pred')fig.canvas.draw()uvp_pred_images.append(np.array(fig.canvas.renderer.buffer_rgba()))plt.close(fig)# 保存为GIF
save_gif(uvp_pred_images, "uvp_pred.gif")print("GIFs were successfully created and saved.")
Remark: 如果用了plt.axis('equal')
后画的图画布留白,可能是因为设置了画布大小fig = plt.figure(figsize=(5, 15))
,把这个去掉直接用fig = plt.figure()
上述代码根据.mat提供的数据生成的gif。(这里是Natural Outflow Boundary Conditions)