一个程序教你花式示爱 1——波动心形线

希望大家使用这个程序时回复一句:愿南宫大大赐予我幸福(滑稽)。和我一样的恩爱狗,我会赐予你们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()

 

  • 7
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值