问题描述
我试图使用Plotly包含两个或两个以上的微分方程图(Scipy.integrate包ODEINT),但它仅绘制初始点而不是整个图。 [这是相应的输出图像I'm tryin to reproduce the plots made in Matplot which is quite trivial,这是完整的代码
import numpy as np
from scipy.integrate import odeint
#import plotly.express as px
from plotly.offline import plot
import plotly.graph_objects as G
# initial condition
y0 = 5.0
# time points
t = np.arange(0,20,1)
# function that returns dy/dt
def model(y,t,k):
dydt = -k * y
return dydt
# solve ODEs
k = 0.1
y1 = odeint(model,y0,args=(k,))
k = 0.2
y2 = odeint(model,))
k = 0.5
y3 = odeint(model,))
fig = G.figure()
#rows,cols = (3,20)
#y = [[0 for i in range(cols)] for j in range(rows)]
for r in [y1,y2,y3]:
fig.add_trace(G.Scatter(
x=t,y=r
)
)
fig.update_layout(title='Sample Graph',xaxis_title='time',yaxis_title='y',template='plotly_white')
# display figure
fig.show()
plot(fig)
解决方法
如果将三个数组(y1
,y2
,y3
展平,则代码将按原样运行;参见下面的示例。
import numpy as np
from scipy.integrate import odeint
from plotly.offline import plot
import plotly.graph_objects as G
# initial condition
y0 = 5.0
# time points
t = np.arange(0,20,1)
# function that returns dy/dt
def model(y,t,k):
dydt = -k * y
return dydt
# solve ODEs
k = 0.1
y1 = odeint(model,y0,args=(k,)).flatten()
k = 0.2
y2 = odeint(model,)).flatten()
k = 0.5
y3 = odeint(model,)).flatten()
fig = G.Figure()
for r in [y1,y2,y3]:
fig.add_trace(G.Scatter(
x=t,y=r
))
fig.update_layout(title='Sample Graph',xaxis_title='time',yaxis_title='y',template='plotly_white')
# display figure
fig.show()