python - Error: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() -
import numpy np import matplotlib.pyplot plt import scipy scipy import interpolate m_c,p_s,complete = np.loadtxt('file1.txt',usecols=(1,0,2),unpack=true) p_d,m_d = np.loadtxt('file2.txt',usecols=(2,3),unpack=true) p_c,m_c = np.loadtxt('file3.txt',usecols=(1,2),unpack=true) def function_oc(m_c,p_c,complete,min,max): average = 0 comp = [] x = 0 while x<8000: if p_c[x]<50 , m_c[x]>=min , m_c[x]<=max: comp.append(complete[x]) x+=1 average = sum(comp)/len(comp) return average average1 = function_oc(m_c,p_c,complete,3,10) average2 = function_oc(m_c,p_c,complete,10,30) average3 = function_oc(m_c,p_c,complete,30,100) average4 = function_oc(m_c,p_c,complete,100,300) average5 = function_oc(m_c,p_c,complete,300,1000) def function_pc(m_d,p_d,m_c,p_c,complete): f= interpolate.interp2d(m_c,p_c,complete) comp_d = f(p_d,m_d) return comp_d comp_d = function_pc(m_d,p_d,m_c,p_c,complete) def function_d(p_d,m_d,min,max): d = 0 = 0 while i<33: if p_d[i]<50 , m_d[i]>=min , m_d[i]<=max: d+=1 i+=1 return d d1 = function_d(p_d,m_d,3,10) d2 = function_d(p_d,m_d,10,30) d3 = function_d(p_d,ms_d,30,100) d4 = function_d(p_d,m_d,100,300) d5 = function_d(p_d,m_d,300,1000) def function_c(p_c,m_c,min,max): c = 0 y = 0 while y<12: if p_c[y]<50 , m_c[y]>=min , m_c[y]<=max: c+=1 y+=1 return c c1 = function_c(p_c,m_c,3,10) c2 = function_c(p_c,m_c,10,30) c3 = function_c(p_c,m_c,30,100) c4 = function_c(p_c,m_c,100,300) c5 = function_c(p_c,m_c,300,1000) ####missed planets in each bin#### def function_m(c_d,p_d,m_d,min,max): m=0 mi in range(len(comp_d)): if p_d[mi]<50 , m_d[mi]>=min , ms_d[mi]<=max: m += 1/comp_d[mi] - 1 return m m1 = function_m(comp_d,p_d,m_d,3,10) m2 = function_m(comp_d,p_dd,m_d,10,30) m3 = function_m(comp_d,p_d,m_d,30,100) m4 = function_m(comp_d,p_d,m_d,100,300) m5 = function_m(comp_d,p_d,m_d,300,1000) occ1 = (d1+c1+m1)/average1 occ2 = (d2+c2+m2)/average2 occ3 = (d3+c3+m3)/average3 occ4 = (d4+c4+m4)/average4 occ5 = (d5+c5+m5)/average5 n = 5 dp = (d1, d2, d3, d4, d5) cp = (c1, c2, c3, c4, c5) mp = (m1, m2, m3, m4, m5) planets = (dp, cp, mp) ind = np.arange(n) width = 0.9 p1 = plt.bar(ind, dp, width, color='red') p2 = plt.bar(ind, cp, width, color='blue', bottom=dp) p3 = plt.bar(ind, mp, width, color='yellow', bottom=[i+j i,j in zip(dp, cp)]) plt.legend((p1[0], p2[0], p3[0]), ('dp', 'cp', 'mp')) plt.show()
i don't understand why error code:
valueerror: truth value of array more 1 element ambiguous. use a.any() or a.all()
the line in code causing issue is:
p3 = plt.bar(ind, mp, width, color='yellow', bottom=[i+j i,j in zip(dp, cp)])
you getting error because trying plot array versus point using plt.bar
. i.e. trying plot ind[0]
versus dp[0]
= dp1
, array. if want this, should use plt.bar
every point in array.
you should use plt.bar
every element in each element in dp
, dp[i][j]
.
Comments
Post a Comment