python - Looping same program for different data files -


for following program, trying save time copying , pasting tons of code. program plot using data file 19_6.txt , aux.19_6, , continue plotting files 11,12,20,28,27, , 18 in 19's place same code , onto same plot. appreciated. thanks!

from numpy import * import matplotlib.pyplot plt  datasim19 = loadtxt("/home/19_6.txt")                          data19 = loadtxt("/home/aux.19_6")                           no1=1 no2=2 no3=3 no4=4 no5=5  no7=7 no8=8 no9=9 no10=10  simrecno1inds19 = nonzero(datasim19[:,1]==no1)[0] simrecno2inds19 = nonzero(datasim19[:,1]==no2)[0] simrecno3inds19 = nonzero(datasim19[:,1]==no3)[0] simrecno4inds19 = nonzero(datasim19[:,1]==no4)[0] simrecno5inds19 = nonzero(datasim19[:,1]==no5)[0]  simrecno7inds19 = nonzero(datasim19[:,1]==no7)[0] simrecno8inds19 = nonzero(datasim19[:,1]==no8)[0] simrecno9inds19 = nonzero(datasim19[:,1]==no9)[0] simrecno10inds19 = nonzero(datasim19[:,1]==no10)[0]   recno1inds19 = nonzero(data19[:,1]==no1)[0] recno2inds19 = nonzero(data19[:,1]==no2)[0] recno3inds19 = nonzero(data19[:,1]==no3)[0] recno4inds19 = nonzero(data19[:,1]==no4)[0] recno5inds19 = nonzero(data19[:,1]==no5)[0]  recno7inds19 = nonzero(data19[:,1]==no7)[0] recno8inds19 = nonzero(data19[:,1]==no8)[0] recno9inds19 = nonzero(data19[:,1]==no9)[0] recno10inds19 = nonzero(data19[:,1]==no10)[0]    q1sim19 = qsim19[simrecno1inds19] q2sim19 = qsim19[simrecno2inds19] q3sim19 = qsim19[simrecno3inds19] q4sim19 = qsim19[simrecno4inds19] q5sim19 = qsim19[simrecno5inds19]  q7sim19 = qsim19[simrecno7inds19] q8sim19 = qsim19[simrecno8inds19] q9sim19 = qsim19[simrecno9inds19] q10sim19 = qsim19[simrecno10inds19]  q1_19 = q19[recno1inds19] q2_19 = q19[recno2inds19] q3_19 = q19[recno3inds19] q4_19 = q19[recno4inds19] q5_19 = q19[recno5inds19]  q7_19 = q19[recno7inds19] q8_19 = q19[recno8inds19] q9_19 = q19[recno9inds19] q10_19 = q19[recno10inds19]  sumq1sim19 = sum(q1sim19) sumq2sim19 = sum(q2sim19) sumq3sim19 = sum(q3sim19) sumq4sim19 = sum(q4sim19) sumq5sim19 = sum(q5sim19)  sumq7sim19 = sum(q7sim19) sumq8sim19 = sum(q8sim19) sumq9sim19 = sum(q9sim19) sumq10sim19 = sum(q10sim19)  sumq1_19 = sum(q1_19) sumq2_19 = sum(q2_19) sumq3_19 = sum(q3_19) sumq4_19 = sum(q4_19) sumq5_19 = sum(q5_19)  sumq7_19 = sum(q7_19) sumq8_19 = sum(q8_19) sumq9_19 = sum(q9_19) sumq10_19 = sum(q10_19)   xsim = [no1, no2, no3, no4, no5, no7, no8, no9, no10] ysim = [sumq1sim_19, sumq2sim_19, sumq3sim_19, sumq4sim_19, sumq5sim_19, sumq7sim_19, sumq8sim_19, sumq9sim_19, sumq10sim_19]  x = [no1, no2, no3, no4, no5,no7, no8, no9, no10] y = [sumq1_19, sumq2_19, sumq3_19, sumq4_19, sumq5_19, sumq7_19, sumq8_19, sumq9_19, sumq10_19]  plt.plot(x,log(y),'b',label='data') plt.plot(xsim,log(ysim),'r',label='simulation') plt.legend() plt.title('data vs. simulation') plt.show() 

tip: when find using lots of variables called n1, n2, n3 etc. should use lists, dictionaries or other such containers, , loops instead.

for example, try replacing following code:

simrecno1inds19 = nonzero(datasim19[:,1]==no1)[0] simrecno2inds19 = nonzero(datasim19[:,1]==no2)[0] simrecno3inds19 = nonzero(datasim19[:,1]==no3)[0] simrecno4inds19 = nonzero(datasim19[:,1]==no4)[0] simrecno5inds19 = nonzero(datasim19[:,1]==no5)[0]  simrecno7inds19 = nonzero(datasim19[:,1]==no7)[0] simrecno8inds19 = nonzero(datasim19[:,1]==no8)[0] simrecno9inds19 = nonzero(datasim19[:,1]==no9)[0] simrecno10inds19 = nonzero(datasim19[:,1]==no10)[0] 

with this:

simrecinds19 = [nonzero(datasim19[:,1] == i)[0] in range(1, 11)] 

then can use simrecinds19[0] instead of simrecno1inds19.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -