绘制3D箭袋图和颂歌

时间:2016-06-21 19:59:01

标签: python matplotlib plot ode

我正在尝试制作3D箭袋图并将其与odeint结合以求解线性化方程。基本上,我想要类似于this但在3D中的东西。我遇到的特殊问题是在代码结束时,当我在做ax.quiver()时,我不断得到“val必须是浮点数或非零浮点数”的错误,我不确定如何解决它。

from scipy.integrate import odeint
from numpy import *
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax =fig.add_subplot(1, 1, 1, projection='3d')
ax.set_xlabel('x')
ax.set_ylabel('u')
ax.set_zlabel('u1')

def testplot(X, t=0,c=0.2):
    x = X[0]
    u = X[1]
    u1=X[2]
    dxdt =x**2*(-1+x+u)*(1-x+(-1+c)*u**2)
    du1dt =c**2*u*(2+x*(-4+2.25*x)+(-4 + 4*x)*u**2 + 2*u**4 + x**2*u*u1)
    dudt=u1*dxdt
    return [dxdt, dudt,du1dt]
X0 = [0.01,0.995,-0.01]#initial values
t = linspace(0, 50, 250)
c=[0.2,0.5,1,2]#changing parameter

for m in c:
    sol = odeint(testplot,X0,t,mxstep=5000000,args=(m,))#solve ode
    ax.plot(sol[:,0],sol[:,1],sol[:,2],lw=1.5,label=r'$c=%.1f$'%m)

x = linspace(-3,3,15)
y = linspace(-4,4,15)
z= linspace(-2,2,15)
x,y,z = meshgrid(x,y,z) #create grid
X,Y,Z = testplot([x,y,z])
M = sqrt(X**2+Y**2+Z**2)#magnitude
M[M==0]=1.
X,Y,Z = X/M, Y/M, Z/M

ax.quiver(x,y,z,X,Y,Z,M,cmap=plt.cm.jet)
ax.minorticks_on()
ax.legend(handletextpad=0,loc='upper left')
setp(ax.get_legend().get_texts(),fontsize=12)
fig.savefig("testplot.svg",bbox_inches="tight",\
        pad_inches=.15)

1 个答案:

答案 0 :(得分:1)

看起来你在ax.quiver()中有一个额外的参数。从我所知,看起来“M”是额外的争论。拿出来,你的箭头看起来像:

ax.quiver(x,y,z,X,Y,Z,cmap=plt.cm.jet)

生成的图像如下所示: enter image description here