在Python中绘制4D连续线

时间:2019-01-12 12:39:04

标签: python python-3.x matplotlib 4d

我在oredr中的Pyhon中使用了solve_ivp函数来求解一个由四个耦合微分方程组成的系统,现在我希望绘制该解以显示其在4-D相空间中接近某个固定点的稳定性。

我当时在想3D空间中的连续线,它也可以根据第4维改变其颜色,这可能是一个很好的解决方案,但找不到任何方法,并且会有所帮助。

>

我用来求解方程式的代码,并将四个向量绘制为时间的函数:

from scipy.integrate import solve_ivp
import numpy as np
import matplotlib.pyplot as plt

#parametres 
Alpha1= 0.4 #in [0,1]
Alpha2= 1 - Alpha1
Kappa=2 
A1=A2=A=0.3
B1=B2=B=0.03
K1 = 2
K2 = 2
Taun = 3.1
Taup = 3.1

# Differential Equations
def V( t , y):
    dN1dt = Alpha1*Kappa + K1*y[1]*(B1-y[0]) - K2*y[0]*(B2-y[1]) -(y[0]/Taun) - A1*( y[0]*(y[2])-(B1-y[0])*y[2]) 
    dN2dt = Alpha2*Kappa + K2*y[0]*(B2-y[1]) - K1*y[1]*(B1-y[0]) -(y[1]/Taun) - A2*( y[1]*(y[3])-(B2-y[1])*y[3]) 
    dS1dt = -(y[2]/Taup) + A1*(y[0]*(y[2]) - (B1-y[0])*y[2]) 
    dS2dt = -(y[3]/Taup) + A2*(y[1]*(y[3]) - (B2-y[1])*y[3]) 
    return [dN1dt, dN2dt, dS1dt, dS2dt] 



# Stable Points
N1ss = B1/2 + 1/(2*A1*Taup)
N2ss = B2/2 + 1/(2*A2*Taup)
S1ss = Alpha1*Kappa*Taup - 0.5*((B*Taup/Taun) + 1/(A*Taun) + (K1-K2)*Taup*(1/(2*(A**2)*(Taup**2)) - 0.5*(B**2)))
S2ss = Alpha2*Kappa*Taup - 0.5*((B*Taup/Taun) + 1/(A*Taun) + (K2-K1)*Taup*(1/(2*(A**2)*(Taup**2)) - 0.5*(B**2)))
print(N1ss,N2ss,S1ss,S2ss)

# Initial Conditions 
N10 = N1ss*(1.1)
N20 = N2ss*(0.9)
S10 = S1ss*(1.5)
S20 = S2ss*(0.3)
y0 = [N10,N20,S10,S20]

#Solution and Plotting
sol = solve_ivp(V, (0,100) , y0)

plt.plot(sol.t, sol.y.T)
plt.legend(['N1', 'N2', 'S1', 'S2'])
plt.xlabel('Time')
plt.ylabel('Excited Dots and Photons')

其中给出了以下情节:

enter image description here

1 个答案:

答案 0 :(得分:1)

对于pyplot,您必须使用具有多个点的散点图来绘制彩色线。您的数据相当稀疏,因此您必须拟合参数化曲线并对其进行采样才能获得更多点,以便可以绘制漂亮的曲线。这很有趣,希望对您有所帮助。

# additional imports

import matplotlib as mpl
from matplotlib import cm as cm
from mpl_toolkits.mplot3d import Axes3D    
from scipy.interpolate import CubicSpline

# include your solving code from above

ts = sol.t
points = sol.y.T
xs = [p[0] for p in points]
ys = [p[1] for p in points]
zs = [p[2] for p in points]
cs = [p[3] for p in points]
colors = cs / max(cs)

csx = CubicSpline(ts, xs)
csy = CubicSpline(ts, ys)
csz = CubicSpline(ts, zs)
csc = CubicSpline(ts, cs)

fine_ts = np.arange(ts[0], ts[-1], 0.01)
fine_xs = [csx(t) for t in fine_ts]
fine_ys = [csy(t) for t in fine_ts]
fine_zs = [csz(t) for t in fine_ts]
fine_cs = [csc(t) for t in fine_ts]
fine_colors = fine_cs / max(fine_cs)

fig = plt.figure(figsize = (10, 10))
ax = fig.gca(projection = '3d')
ax.plot(xs, ys, zs)
ax.scatter(fine_xs, fine_ys, fine_zs, c = cm.hot(fine_colors))
plt.show()

Your curve