Home > OS >  How to use a for loop to calculate and plot multiple lines on one graph
How to use a for loop to calculate and plot multiple lines on one graph

Time:03-02

I am fairly inexperienced when it comes to coding & I need help plotting multiple lines on one graph. I am using a diffeq solver and need to input different values for a constant that feeds into other equations and plot the solution for each value of the constant (in this case I will be looking at Ha from 6x10^3-6x10^8, changing by x10 each time). I will paste some of my code below, & would greatly appreciate any help!

IEPOXg_i = 1*10**-9
IEPOXaq_i = 0  
T = 280.0
Ptot = 1 
R = 0.082057
V = 1 
Rp = 100.0*10**(-7) 
Dg = 0.1
k= 0 
a = 0.1  
Ma = 0.1191390 
Daq = 10.0**-9 
Wl = 1*10**-6 
Ha = 6*10**3 
q = Rp*((k/Daq)**0.5) 
Raq = 0 
w = ((8*R*T)/(3.1415*Ma))**0.5 
Kmt = ((Rp**2/3/Dg) (4*Rp/3/w/a))**(-1) 
IEPOXg = IEPOXg_i*Ptot
IEPOXaq = ((Kmt*IEPOXg_i)/(((Kmt/Ha)) (3*(k**0.5)*(Daq**0.5)/Rp))) 

y0 = [IEPOXg_i, IEPOXaq_i] 
t = 3600 * np.arange(0, 24, 1) 
def IEPOX_ODE(y, t, Kmt, Wl, Raq, Ha): 
  IEPOXg, IEPOXaq = y
  dydt = [-(Kmt*Wl*IEPOXg)   ((1/Ha)*Kmt*IEPOXaq*Wl), ((Kmt*IEPOXg)/(R*T))-((Kmt*IEPOXaq)/(Ha*R*T))-(Q*Raq)]
  return dydt
time_list = np.arange(0, 1, 0.01)
from scipy.integrate import odeint
sol1 = odeint(IEPOX_ODE, y0, time_list, args=(Kmt, Wl, Raq, Ha))
print(sol1)

plt.yscale("log")
plt.plot(sol1[:,0]/(T*(R*10**(-3))), label = 'IEPOXg') #IEPOXg/(T*R*0.001) = atm / k * 
L*atm/k*mol * L/m3 = mol/m3
plt.plot(sol1[:,1]*Wl*1000, label = 'IEPOXaq') 
plt.legend()
plt.title('')
plt.xlabel('')
plt.ylabel('')

CodePudding user response:

You put the integration and plotting in a loop.

plt.yscale("log")
for Ha in np.logspace(3,6,4):
    sol1 = odeint(IEPOX_ODE, y0, time_list, args=(Kmt, Wl, Raq, Ha))
    # print(sol1)

    plt.plot(sol1[:,0]/(T*(R*10**(-3))), label = 'IEPOXg Ha=%.2g'%Ha) 
    # IEPOXg/(T*R*0.001) = atm / k * L*atm/k*mol * L/m3 = mol/m3
    plt.plot(sol1[:,1]*Wl*1000, label = 'IEPOXaq Ha=%.2g'%Ha) 
plt.legend()

Also do explore the plt.subplots command to distribute the different graphs to different plots with their own adaptation of the coordinate axes.

  • Related