首页 > 解决方案 > 在 Python 中绘制 4-D 连续线

问题描述

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

我在想 3-D 空间中的一条连续线,它也会根据第 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')

这给出了以下情节:

在此处输入图像描述

标签: pythonpython-3.xmatplotlib4d

解决方案


使用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()

你的曲线


推荐阅读