简体   繁体   English

在 Python 中为循环创建多个图

[英]Creating multiple plots in Python for loop

I want to create a plot of (T/Tmax vs R/R0) for different values of Pa in a single plot like below.我想在单个 plot 中为不同的 Pa 值创建(T/Tmax vs R/R0)的 plot,如下所示。 I have written this code that appends values to a list but all values of (T/Tmax vs R/R0) are appended in single list which does not give a good plot.我已经编写了将值附加到列表的代码,但是(T/Tmax vs R/R0)的所有值都附加在单个列表中,这并不能提供良好的 plot。 What can I do to have such a plot?我该怎么做才能拥有这样的 plot? Also how can I make an excel sheet from the data from the loop where column 1 is T/Tmax list and column 2,3,4...are corresponding R/R0 values for different pa?另外,如何从循环中的数据制作 excel 表,其中第 1 列是 T/Tmax 列表,第 2、3、4 列是不同的 pa 对应的 R/R0 值?

示例图

KLMDAT1 = []

KLMDAT2 = []

for j in range(z):

    pa[j] = 120000-10000*j

    i = 0
    R = R0
    q = 0
    T = 0

    while (T<Tmax):

        k1 = KLM_RKM(i*dT,R,q,pa[j])
        k2 = KLM_RKM((i+0.5)*dT,R,q+0.5*dT*k1,pa[j])
        k3 = KLM_RKM((i+0.5)*dT,R,q+0.5*dT*k2,pa[j])
        k4 = KLM_RKM((i+1)*dT,R,q+dT*k3,pa[j])

        q = q +1/6.0*dT*(k1+2*k2+2*k3+k4)
        R = R+dT*q

        if(R>0):

            KLMDAT1.append(T / Tmax)
            KLMDAT2.append(R / R0)

        if(R>Rmax):
                Rmax = R

        if (abs(q)>c or R < 0):
                    break
        T=T+dT
        i = i+1

wb.save('KLM.xlsx')

np.savetxt('KLM.csv',[KLMDAT1, KLMDAT2])

plt.plot(KLMDAT1, KLMDAT2)

plt.show()

You are plotting it wrong.你画错了。 Your first variable needs to be T/Tmax.您的第一个变量需要是 T/Tmax。 So initialize an empty T list, append T values to it, divide it by Tmax, and then plot twice: first KLMDAT1 and then KLMDAT2 .所以初始化一个空的 T 列表,append T 值给它,除以 Tmax,然后 plot 两次:首先KLMDAT1 ,然后KLMDAT2 Following pseudocode explains it以下伪代码解释它

KLMDAT1 = []
KLMDAT2 = []
T_list = [] # <--- Initialize T list here

for j in range(z):
    ...

    while (T<Tmax):
        ...

        T=T+dT
        T_list.append(T) # <--- Append T here
        i = i+1

# ... rest of the code

plt.plot(np.array(T_list)/Tmax, KLMDAT1) # <--- Changed here
plt.plot(np.array(T_list)/Tmax, KLMDAT2) # <--- Changed here

plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM