我试图为不同的mu和sigma值绘制直线图(漂移布朗运动),我有一个函数,它迭代可能的mu值和可能的sigma值的列表,然后它应该返回结果图。问题是我不确定如何使subplots
返回所需的行数。我已经给了它正确的nrows
和ncols
,但问题在于索引。有人有办法解决这个问题吗
我已经在下面提供了代码和错误消息
# Drifted BM for varying values mu and sigma respectively
def DriftedBMTest2(nTraj=50,T=5.0,dt=0.01,n=5, sigma = [0.1,1.0,2], mulist=[0,0.5,1,1.5], ValFSize=(18,14)):
nMu = len(mulist)
nSigma = len(mulist)
# Discretize, dt = time step = $t_{j+1}- t_{j}$
dt = T/(n-1)
# Loop on different value sigma
for z in range(nSigma):
# Loop on different value Mu
for k in range(nMu):
n=int(T/dt)
x=np.zeros(n+1,float)
# Create plot space
temp = nSigma*nMu/2
plt.subplot(temp,2,k+1)
plt.title("Drifted BM $\sigma$={}, $\mu$={}".format(sigma[z],mulist[k]))
plt.xlabel(r'$t$')
plt.ylabel(r'$W_t$');
# Container for colours for each trajectory
colors = plt.cm.jet(np.linspace(0,1,nTraj))
# Generate many trajectories
for j in range(nTraj):
# Time simulation
# Add the time * constant(mu)
for i in range(n):
x[i+1]=x[i]+np.sqrt(dt)*np.random.randn() + i*mulist[k]
# Scale Each Tradjectory
x = x * sigma[z]
# Plot trajectory just computed
plt.plot(np.linspace(0,T,n+1),x,'b-',alpha=0.3, color=colors[j], lw=3.0)
DriftedBMTest2( sigma = [1,2], mulist=[-2,1] )
然后我得到了前两个图,但不是全部,还有下面的错误
MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
很抱歉,如果这是一个糟糕的问题,我是Python新手,但任何帮助都将不胜感激
尝试在两个for循环之间添加
fig = plt.figure()
如果这不能提供所需的布局,您可以尝试将其移动到内部for循环,如下所示
相关问题 更多 >
编程相关推荐