【问题标题】:Plotting multiple subplots on same graph在同一张图上绘制多个子图
【发布时间】:2016-12-12 00:24:39
【问题描述】:
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 循环中绘制内容。我听说 subplots 是最好的方法,但我对实现不熟悉,可以使用一些帮助。谢谢!

【问题讨论】:

  • 立即运行您的代码。需要一段时间才能运行。同时,我通常将 plt.draw() 或 plt.show() 移到循环之外。

标签: python matplotlib plot subplot


【解决方案1】:

取消缩进最后一个命令对我有用。

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

【讨论】:

  • 感谢您的及时回复。我也来到了这个情节,但几乎立即放弃了它,因为它似乎只显示了 1.05 和 1.15 之间的值,这似乎不正确。
  • 这是你要找的吗?
  • 只要这是为 a_array 中的 a 的特定值生成 w 数组,然后为 a 的每个值在同一张图上绘制结果,那么是的,这就是我要找的
  • 那么这应该可以解决您的问题。随意将其作为公认的解决方案推销,否则请告诉我是否有可以进一步澄清的内容。
猜你喜欢
  • 2017-04-04
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
  • 2012-01-14
  • 1970-01-01
  • 2012-11-24
  • 1970-01-01
相关资源
最近更新 更多