在同一图表上绘制多个子图

时间:2016-12-12 00:24:40

标签: python matplotlib plot subplot

from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import math
from scipy.integrate import odeint
from scipy.fftpack import fft, ifft

def pend(y, t, a, b, ohm):
    theta, omega, phi = y
    dydt = [omega, -b*omega-np.sin(theta)-a*np.cos(phi), ohm]
    return dydt

b = 1.0/2.0      #beta
ohm = 2.0/3.0        #capital Omega
period = 2.0*math.pi/ohm       #driving period

t0 = 0.0       #initial time 
t = np.linspace(t0,t0+period*10**3,10**3+1)       #time for Poincare map

theta0 = 0.75
omega0 = 1.6
phi0 = 0.8
y0 = [theta0,omega0,phi0]       #initial conditions
N = 100                         #number of transient points to delete

a_array = np.linspace(0,1.15,50)   #varying parameter of a values

for a in a_array:
    sol = odeint(pend,y0,t,args=(a,b,ohm))     #numerical integration of differential equation
    sol = sol[N:10**3-N]     #removing transients
    w = sol[:,1]             #frequency
    A = np.full(len(w),a)      #array of a-values
    plt.plot(A, w)
    plt.draw()

我正在尝试构建一个分叉图。在我们使用的方程组中,a是控制参数,我们在x轴上绘制0到1.15之间的值,而对于a的特定值,绘制值数组(称为w)。我不太确定如何在这样的for循环中绘制事物。我听说子图是最好的方法,但我不熟悉实现,可以使用一些帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

Unindenting最后一个命令对我有用。

from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import math
from scipy.integrate import odeint
from scipy.fftpack import fft, ifft

def pend(y, t, a, b, ohm):
    theta, omega, phi = y
    dydt = [omega, -b*omega-np.sin(theta)-a*np.cos(phi), ohm]
    return dydt

b = 1.0/2.0      #beta
ohm = 2.0/3.0        #capital Omega
period = 2.0*math.pi/ohm       #driving period

t0 = 0.0       #initial time 
t = np.linspace(t0,t0+period*10**3,10**3+1)       #time for Poincare map

theta0 = 0.75
omega0 = 1.6
phi0 = 0.8
y0 = [theta0,omega0,phi0]       #initial conditions
N = 100                         #number of transient points to delete

a_array = np.linspace(0,1.15,50)   #varying parameter of a values

for a in a_array:
    sol = odeint(pend,y0,t,args=(a,b,ohm))     #numerical integration of differential equation
    sol = sol[N:10**3-N]     #removing transients
    w = sol[:,1]             #frequency
    A = np.full(len(w),a)      #array of a-values
    plt.plot(A, w)
plt.show()

enter image description here