# -*- coding: utf-8 -*-
"""
Created on Fri Nov 11 18:41:30 2022
fluent鱼儿游动,保存为gif动图
@author: in
"""
import matplotlib
matplotlib.use('Agg') # 只绘图保存,不显示图片,一定要加在import后面
import numpy as np
import matplotlib.pyplot as plt
import imageio
import pandas as pd
from matplotlib.pyplot import MultipleLocator#从pyplot导入MultipleLocator类,这个类用于设置刻度间隔
path = r'D:\20221112fish\move\coordinate.xlsx'
data = pd.DataFrame(pd.read_excel(path))
output_path = r'D:\20221112fish\move\test.gif'#保存gif的路径及名字
print(data.index)#获取行的索引名称
print(data.columns)#获取列的索引名称
x = data['x'].values.tolist()#获取列名为姓名这一列的内容
t = data['t'].values.tolist()#获取列名为姓名这一列的内容
b = data['b'].values.tolist()#获取列名为姓名这一列的内容
m = data['m'].values.tolist()
time = 1.8
step = 0.04
image_list = []
pig = 0
zdfd = 1
def fish(x,t,dt,zdfd):
k = 2*3.1415926/0.95
w = 2*3.1415926*1
return zf*(0.02-0.08*x+0.16*x*x)*((np.sin(k*x-w*t))-(np.sin(k*x-w*(t-dt))))
for td in np.arange(0.01,time,step):
pig = pig+1
ty = []
my = []
by = []
for xp in range(0,len(x)):
dy = fish(x[xp],td,step,zdfd)
t[xp] = t[xp]+dy
m[xp] = m[xp]+dy
b[xp] = b[xp]+dy
plt.plot(x, t)
plt.plot(x, b)
plt.plot(x, m)
'''设置数据在四周的坐标显示'''
plt.tick_params(top=True,bottom=True,left=True,right=True)
plt.tick_params(labeltop=True,labelleft=True,labelright=True,labelbottom=True)
plt.grid( color = 'black',linestyle='-.',linewidth = 1)#设置网格线
plt.ylim(-0.3,0.3)#设置y轴的最大值和最小值
# plt.xlim(-0.5,11)#设置x轴的最大值和最小值
# '''设置x,y轴的间隔'''
# x_major_locator=MultipleLocator(0.1)
# #把x轴的刻度间隔设置为,并存在变量里
# y_major_locator=MultipleLocator(0.05)
# #把y轴的刻度间隔设置为,并存在变量里
# ax=plt.gca()
# #ax为两条坐标轴的实例
# ax.xaxis.set_major_locator(x_major_locator)
# #把x轴的主刻度设置为0.1
# ax.yaxis.set_major_locator(y_major_locator)
# #把y轴的主刻度设置为0.05
plt.savefig('temp.png')
img = cv2.imread('temp.png')
image_list.append(imageio.imread('temp.png'))
plt.pause(0.1)
plt.close()
imageio.mimsave(output_path, image_list, 'GIF', duration=0.35)
excel的链接:https://download.csdn.net/download/weixin_43245453/86996059