Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在for循环中使用';等一下';matplotlib中的效果?_Python_Matlab_Matplotlib_Plot - Fatal编程技术网

Python 在for循环中使用';等一下';matplotlib中的效果?

Python 在for循环中使用';等一下';matplotlib中的效果?,python,matlab,matplotlib,plot,Python,Matlab,Matplotlib,Plot,使用Matplotlib,我希望得到一个连接多个点的绘图。绘图命令位于for循环中。现在,我每个人都得到一个数字,必须关闭第一个数字才能打开第二个数字 预期效果如下图所示: 每个点在偶数N处 如何通过修改当前的Python代码来实现这一点?重要的部分是最后4行,以及第7行的第一个for循环 代码: import numpy as np import matplotlib.pyplot as plt from scipy.integrate import odeint y = 5 x = 2

使用Matplotlib,我希望得到一个连接多个点的绘图。绘图命令位于for循环中。现在,我每个人都得到一个数字,必须关闭第一个数字才能打开第二个数字

预期效果如下图所示:

每个点在偶数N处

如何通过修改当前的Python代码来实现这一点?重要的部分是最后4行,以及第7行的第一个for循环

代码:

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint

y = 5
x = 2
for N in range(x,x+y,2):
    #Constants and parameters
    epsilon = 0.01
    K00 = np.logspace(0,3,10,10)
    len1 = len(K00)
    y0 = [0]*(3*N/2+3)
    Kplot = np.zeros((len1,1))
    Pplot = np.zeros((len1,1))
    S = [np.zeros((len1,1)) for kkkk in range(N/2+1)]
    KS = [np.zeros((len1,1)) for kkkk in range(N/2)]
    PS = [np.zeros((len1,1)) for kkkk in range(N/2)]
    Splot = [np.zeros((len1,1)) for kkkk in range(N/2+1)]
    KSplot = [np.zeros((len1,1)) for kkkk in range(N/2)]
    PSplot = [np.zeros((len1,1)) for kkkk in range(N/2)]

    for series in range(0,len1):
        K0 = K00[series]
        Q = 10
        r1 = 0.0001
        r2 = 0.001
        a = 0.001
        d = 0.001
        k = 0.999
        S10 = 1e5
        P0 = 1
        tf = 1e10
        time = np.linspace(0,tf,len1)

        #Defining dy/dt's
        def f(y,t):
            for alpha in range(0,(N/2+1)):
                S[alpha] = y[alpha]
            for beta in range((N/2)+1,N+1):
                KS[beta-N/2-1] = y[beta]
            for gamma in range(N+1,3*N/2+1):
                PS[gamma-N-1] = y[gamma]
            K = y[3*N/2+1]
            P = y[3*N/2+2]

            # The model equations
            ydot = np.zeros((3*N/2+3,1))
            B = range((N/2)+1,N+1)
            G = range(N+1,3*N/2+1)
            runsumPS = 0
            runsum1 = 0
            runsumKS = 0
            runsum2 = 0

            for m in range(0,N/2):
                runsumPS = runsumPS + PS[m]
                runsum1 = runsum1 + S[m+1]
                runsumKS = runsumKS + KS[m]
                runsum2 = runsum2 + S[m]
                ydot[B[m]] = a*K*S[m]-(d+k+r1)*KS[m]

            for i in range(0,N/2-1):
                ydot[G[i]] = a*P*S[i+1]-(d+k+r1)*PS[i]

            for p in range(1,N/2):
                ydot[p] = -S[p]*(r1+a*K+a*P)+k*KS[p-1]+d*(PS[p-1]+KS[p])

            ydot[0] = Q-(r1+a*K)*S[0]+d*KS[0]+k*runsumPS
            ydot[N/2] = k*KS[N/2-1]-(r2+a*P)*S[N/2]+d*PS[N/2-1]
            ydot[G[N/2-1]] = a*P*S[N/2]-(d+k+r2)*PS[N/2-1]
            ydot[3*N/2+1] = (d+k+r1)*runsumKS-a*K*runsum2
            ydot[3*N/2+2] = (d+k+r1)*(runsumPS-PS[N/2-1])- \
                            a*P*runsum1+(d+k+r2)*PS[N/2-1]

            ydot_new = []
            for j in range(0,3*N/2+3):
                ydot_new.extend(ydot[j])
            return ydot_new

        # Initial conditions
        y0[0] = S10
        for i in range(1,3*N/2+1):
            y0[i] = 0
        y0[3*N/2+1] = K0
        y0[3*N/2+2] = P0

        # Solve the DEs
        soln = odeint(f,y0,time, mxstep = 5000)
        for alpha in range(0,(N/2+1)):
            S[alpha] = soln[:,alpha]
        for beta in range((N/2)+1,N+1):
            KS[beta-N/2-1] = soln[:,beta]
        for gamma in range(N+1,3*N/2+1):
            PS[gamma-N-1] = soln[:,gamma]

        for alpha in range(0,(N/2+1)):
            Splot[alpha][series] = soln[len1-1,alpha]
        for beta in range((N/2)+1,N+1):
            KSplot[beta-N/2-1][series] = soln[len1-1,beta]
        for gamma in range(N+1,3*N/2+1):
            PSplot[gamma-N-1][series] = soln[len1-1,gamma]

        u1 = 0
        u2 = 0
        u3 = 0

        for alpha in range(0,(N/2+1)):
            u1 = u1 + Splot[alpha]
        for beta in range((N/2)+1,N+1):
            u2 = u2 + KSplot[beta-N/2-1]
        for gamma in range(N+1,3*N/2+1):
            u3 = u3 + PSplot[gamma-N-1]

        K = soln[:,3*N/2+1]
        P = soln[:,3*N/2+2]
        Kplot[series] = soln[len1-1,3*N/2+1]
        Pplot[series] = soln[len1-1,3*N/2+2]
        utot = u1+u2+u3

    #Plot
    Kcrit = abs((Q/r2)*(1+epsilon)-utot)
    v,i = Kcrit.min(0),Kcrit.argmin(0)
    plt.plot(N,K00[i])
    plt.show()

谢谢您的帮助。

如果我是正确的,您只需要一个包含所有计算点的绘图。如果是这种情况,简单的方法是存储所有点,并在最后绘制它们。所以我要做的就是。需要创建两个列表来存储数据x_data_plot和y_data_plot。因此,这些变化将是:

创建存储的列表

# The data of the plot will be added in these lists
x_data_plot=[]
y_data_plot=[]
在循环的每个迭代中存储数据

# Save the new points for x and y
x_data_plot.append(N)
y_data_plot.append(K00[i])
最后,我们来设计这个情节

# Make the plot of all the points together
plt.plot(x_data_plot,y_data_plot)
plt.show()
综合

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint

y = 5
x = 2
# The data of the plot will be added in these lists
x_data_plot=[]
y_data_plot=[]
for N in range(x,x+y,2):
    #Constants and parameters
    epsilon = 0.01
    K00 = np.logspace(0,3,10,10)
    len1 = len(K00)
    y0 = [0]*(3*N/2+3)
    Kplot = np.zeros((len1,1))
    Pplot = np.zeros((len1,1))
    S = [np.zeros((len1,1)) for kkkk in range(N/2+1)]
    KS = [np.zeros((len1,1)) for kkkk in range(N/2)]
    PS = [np.zeros((len1,1)) for kkkk in range(N/2)]
    Splot = [np.zeros((len1,1)) for kkkk in range(N/2+1)]
    KSplot = [np.zeros((len1,1)) for kkkk in range(N/2)]
    PSplot = [np.zeros((len1,1)) for kkkk in range(N/2)]

    for series in range(0,len1):
        K0 = K00[series]
        Q = 10
        r1 = 0.0001
        r2 = 0.001
        a = 0.001
        d = 0.001
        k = 0.999
        S10 = 1e5
        P0 = 1
        tf = 1e10
        time = np.linspace(0,tf,len1)

        #Defining dy/dt's
        def f(y,t):
            for alpha in range(0,(N/2+1)):
                S[alpha] = y[alpha]
            for beta in range((N/2)+1,N+1):
                KS[beta-N/2-1] = y[beta]
            for gamma in range(N+1,3*N/2+1):
                PS[gamma-N-1] = y[gamma]
            K = y[3*N/2+1]
            P = y[3*N/2+2]

            # The model equations
            ydot = np.zeros((3*N/2+3,1))
            B = range((N/2)+1,N+1)
            G = range(N+1,3*N/2+1)
            runsumPS = 0
            runsum1 = 0
            runsumKS = 0
            runsum2 = 0

            for m in range(0,N/2):
                runsumPS = runsumPS + PS[m]
                runsum1 = runsum1 + S[m+1]
                runsumKS = runsumKS + KS[m]
                runsum2 = runsum2 + S[m]
                ydot[B[m]] = a*K*S[m]-(d+k+r1)*KS[m]

            for i in range(0,N/2-1):
                ydot[G[i]] = a*P*S[i+1]-(d+k+r1)*PS[i]

            for p in range(1,N/2):
                ydot[p] = -S[p]*(r1+a*K+a*P)+k*KS[p-1]+d*(PS[p-1]+KS[p])

            ydot[0] = Q-(r1+a*K)*S[0]+d*KS[0]+k*runsumPS
            ydot[N/2] = k*KS[N/2-1]-(r2+a*P)*S[N/2]+d*PS[N/2-1]
            ydot[G[N/2-1]] = a*P*S[N/2]-(d+k+r2)*PS[N/2-1]
            ydot[3*N/2+1] = (d+k+r1)*runsumKS-a*K*runsum2
            ydot[3*N/2+2] = (d+k+r1)*(runsumPS-PS[N/2-1])- \
                            a*P*runsum1+(d+k+r2)*PS[N/2-1]

            ydot_new = []
            for j in range(0,3*N/2+3):
                ydot_new.extend(ydot[j])
            return ydot_new

        # Initial conditions
        y0[0] = S10
        for i in range(1,3*N/2+1):
            y0[i] = 0
        y0[3*N/2+1] = K0
        y0[3*N/2+2] = P0

        # Solve the DEs
        soln = odeint(f,y0,time, mxstep = 5000)
        for alpha in range(0,(N/2+1)):
            S[alpha] = soln[:,alpha]
        for beta in range((N/2)+1,N+1):
            KS[beta-N/2-1] = soln[:,beta]
        for gamma in range(N+1,3*N/2+1):
            PS[gamma-N-1] = soln[:,gamma]

        for alpha in range(0,(N/2+1)):
            Splot[alpha][series] = soln[len1-1,alpha]
        for beta in range((N/2)+1,N+1):
            KSplot[beta-N/2-1][series] = soln[len1-1,beta]
        for gamma in range(N+1,3*N/2+1):
            PSplot[gamma-N-1][series] = soln[len1-1,gamma]

        u1 = 0
        u2 = 0
        u3 = 0

        for alpha in range(0,(N/2+1)):
            u1 = u1 + Splot[alpha]
        for beta in range((N/2)+1,N+1):
            u2 = u2 + KSplot[beta-N/2-1]
        for gamma in range(N+1,3*N/2+1):
            u3 = u3 + PSplot[gamma-N-1]

        K = soln[:,3*N/2+1]
        P = soln[:,3*N/2+2]
        Kplot[series] = soln[len1-1,3*N/2+1]
        Pplot[series] = soln[len1-1,3*N/2+2]
        utot = u1+u2+u3

    #Plot
    Kcrit = abs((Q/r2)*(1+epsilon)-utot)
    v,i = Kcrit.min(0),Kcrit.argmin(0)
    # Save the new points for x and y
    x_data_plot.append(N)
    y_data_plot.append(K00[i])

# Make the plot of all the points together
plt.plot(x_data_plot,y_data_plot)
plt.show()
这将导致:


如果你想要一张恐龙图像,那就要复杂得多,但这是可能的。自讨苦吃。

谢谢。这正是我想要的!动态图像是什么意思?@横坐标我指的是某种动画。因此,您将从一个空图像开始,并在计算新值时将其添加到绘图中。当您执行脚本时,您将看到一个空绘图,并且点将逐个显示。最后,你将得到与答案中的脚本完全相同的情节。