matplotlib绘制拟合大象曲线
通过十个参数绘制大象曲线
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
num=101
k=np.arange(1,6)
t=np.linspace(0,2*np.pi,num)
Ax=[0,0,12,0,-14]
Bx=[50,18,0,0,0]
Ay=[-60,0,0,0,0]
By=[-30,8,-10,0,0]
eye=20
x=(np.matmul(np.array(Ax).reshape(1,5),np.cos(k.reshape(5,1)*t.reshape(1,101)))+np.matmul(np.array(Bx).reshape(1,5),np.sin(k.reshape(5,1)*t.reshape(1,101)))).reshape((101,))
y=(np.matmul(np.array(Ay).reshape(1,5),np.cos(k.reshape(5,1)*t.reshape(1,101)))+np.matmul(np.array(By).reshape(1,5),np.sin(k.reshape(5,1)*t.reshape(1,101)))).reshape((101,))
plt.plot(y,-x,eye,eye,'o')
plt.show()
让大象的鼻子动起来
num=101
k=np.arange(1,6)
t=np.linspace(0,2*np.pi,num)
Ax=[-60,0,0,0,0]
Bx=[-30,8,-10,0,0]
Ay=[0,0,-12,0,14]
By=[-50,-18,0,0,0]
eye=20
wigg=40
x=(np.matmul(np.array(Ax).reshape(1,5),np.cos(k.reshape(5,1)*t.reshape(1,101)))+np.matmul(np.array(Bx).reshape(1,5),np.sin(k.reshape(5,1)*t.reshape(1,101)))).reshape((101,))
y=(np.matmul(np.array(Ay).reshape(1,5),np.cos(k.reshape(5,1)*t.reshape(1,101)))+np.matmul(np.array(By).reshape(1,5),np.sin(k.reshape(5,1)*t.reshape(1,101)))).reshape((101,))
A=20
nose=np.where(x>wigg)
nose_m=np.where(x>=np.max(x))
class Scope:
def __init__(self,ax):
self.ax=ax
self.move_num=100
self.i=0
def paint(self,j):
if self.i==self.move_num:
self.i=0
y0=np.zeros((101,))
# import pdb;pdb.set_trace()
y0[nose]=A*np.sin(2*np.pi*self.i/self.move_num)*np.exp(-np.power((nose[0]-nose_m[0]),2)/40)
self.ax.cla()
self.ax.plot(x,y+y0,eye,eye,'o')
self.i+=1
fig,ax=plt.subplots()
scope=Scope(ax)
ani=animation.FuncAnimation(fig,scope.paint,interval=100)
plt.show()
参考链接:
在Python MatPlotLib中使用动画时,如何更改绘制曲线的颜色?
创建于2407101112,修改于2407101112