希望大家使用这个程序时回复一句:愿南宫大大赐予我幸福(滑稽)。和我一样的恩爱狗,我会赐予你们i幸福,单身狗的话我会赐予你一个女朋友(当然是虚拟的,滑稽)。当然咯,函数生成的图片我才不会给你们看呢,这是我和我女票的。
PS:程序如果运行出错,多半属于没有及时更新matplotlib函数库,请使用如下语句在cmd控制台安装:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -U matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
ln, = ax.plot([], [], '-',color='r', lw=1)
time_template = 'LOVE = %.1fs'
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
def init():
ax.set_xlim(-3,3)
ax.set_ylim(-2, 3)
return ln,
def update(ii):
xdata, ydata = [], []
for i in range(0,183):
xi=(182-i)/100
xdata.append(0.01*i-1.82)
yi=(xi**(2/3))+(0.9*(3.3-xi**2)**0.5)*np.cos(ii*(np.pi)*xi)
if type(yi)=='complex':
yi=np.around(abs(yi),decimals=4)
yi=np.around(yi, decimals=3)
ydata.append(yi)
for i in range(0,182):
xi=i/100
xdata.append(xi)
yi=(xi**(2/3))+(0.9*(3.3-xi**2)**0.5)*np.cos(ii*(np.pi)*xi)
if type(yi)=='complex':
yi=np.around(abs(yi),decimals=4)
yi=np.around(yi, decimals=3)
ydata.append(yi)
ln.set_data(xdata, ydata)
time_text.set_text(time_template %(ii))
return ln,
ani = FuncAnimation(fig, update, np.linspace(0, 13.14, 100),init_func=init, interval=100)
ani.save('love.gif', writer='imagemagick', fps=10)
plt.show()