如何用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、结果

经验分享 程序员 微信小程序 职场和发展