如何用python画一个动态的红心

1、源码

import numpy as np
import matplotlib.pyplot as plt
import math
from scipy.fftpack import fft,ifft
import mpl_toolkits.mplot3d
from decimal import Decimal
from matplotlib.animation import FuncAnimation

plt.figure(1)
#x=[-2:.001:2]
x = np.linspace(-2,2,800)
y=(np.sqrt(np.cos(x)) * np.cos(100*x)+np.sqrt(abs(x))-0.0) * (10-x*x) **(0.01)
plt.plot(x,y)
plt.show()

plt.figure(2)
DT = 0.001  # time tick [s]
SIM_TIME = 300*0.001#40.0  # simulation time [s]

show_animation = True

time = 0.0  

fig, ax = plt.subplots()
i =0
h_y_true = np.array([])
h_x_true = np.array([])
xdata, ydata = [], [] 
ln, = ax.plot([], [], 'r-', animated=False) 

def init():
    ax.set_xlim(-2, 2)
    ax.set_ylim(-1, 2)
    return ln,


def update(n):
    xdata.append(n)         #将每次传过来的n追加到xdata中
    ydata.append((np.sqrt(np.cos(n)) * np.cos(100*n)+np.sqrt(abs(n))-0.0) * (10-n*n) **(0.01))
    ln.set_data(xdata, ydata)    #重新设置曲线的值
    return ln,

anim = FuncAnimation(fig, update, frames=x, interval=10,init_func=init, blit=True)
#if len(sys.argv) > 1 and sys.argv[1] == 'save':
anim.save('line.gif', dpi=80, writer='imagemagick',)
plt.show()

2、结果
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值