0
import numpy as np
import matplotlib.pyplot as plt
import scipy
from 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 and m_c[x]>=min and 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
    i = 0
    while i<33:
        if p_d[i]<50 and m_d[i]>=min and 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 and m_C[y]>=min and 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
    for mi in range(len(comp_d)):
        if p_d[mi]<50 and m_d[mi]>=min and 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 for i,j in zip(dp, cp)])
plt.legend((p1[0], p2[0], p3[0]), ('DP', 'CP', 'MP'))
plt.show()

I don't understand why I get this error for my code:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

The line in the code that is causing this issue is:

p3 = plt.bar(ind, mp, width, color='yellow', bottom=[i+j for i,j in zip(dp, cp)])

3 Answers3

2

This error arises when you do something like:

if a < b:

when a or b is an array.

I can't trace where this might be since I don't have your input text files (and you haven't provided the full error trace), but you have a lot of if statements that are potential culprits.

The problem is that a < b in the case of an array resolves to an array of boolean values, for example,

array([True, True, False]) 

which the if can't parse. np.any and np.all will parse the array of booleans to, as per my example, True for np.any and False for np.all.

Daniel F
  • 13,620
  • 2
  • 29
  • 55
2

You have to use:

np.logical_or(a,b)
np.logical_and(a,b)

for np arrays. It works really well for me!

Hao Xu
  • 449
  • 1
  • 5
  • 8
1

You are getting this error because you are trying to plot an array versus a point using plt.bar. I.e. you are trying to plot ind[0] versus dp[0] = dp1, which is an array. If you want to do this, you should use plt.bar for every point in the array.

You should use plt.bar for every element in each element in dp, so for dp[i][j].

Daniel F
  • 13,620
  • 2
  • 29
  • 55
Rein K.
  • 141
  • 8