Won´t plot my funktion

hello guys, i have a problem and don´t know how to solve it. the error is: “The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()”

code:

import math
from matplotlib import pyplot as plt
import numpy as np

def local_atmospheric_temperature(altitude):
    
    h = altitude # [m]
    #Constants
    ##referenz altitude [m]
    h_0= 0
    h_1= 11000
    h_2= 20000
    h_3= 32000
    h_4= 47000
    h_5= 51000
    h_6= 71000
    
    ##referenz temperature [K]
    T_0= 288.15
    T_1= 216.65
    T_2= 216.65
    T_3= 228.65
    T_4= 270.65
    T_5= 270.65
    T_6= 214.65
    
    ##descent rate [K/m]
    L_0= -0.0065
    L_1= 0
    L_2= 0.001
    L_3= 0.0028
    L_4= 0
    L_5= -0.0028
    L_6= -0.002

    #Troposphere
    if( h_0 <= h < h_1):
        T_b = T_0
        L_b = L_0
        h_b = h_0
    #tropopause
    elif(h_1 <= h < h_2):
        T_b = T_1
        L_b = L_1
        h_b = h_1
    #stratosphere1
    elif(h_2 <= h < h_3):
        T_b = T_2
        L_b = L_2
        h_b = h_2
    #stratosphere2
    elif(h_3 <= h < h_4):
        T_b = T_3
        L_b = L_3
        h_b = h_3
    #stratopause
    elif(h_4 <= h < h_5):
        T_b = T_4
        L_b = L_4
        h_b = h_4
    #mesosphere1    
    elif(h_5 <= h < h_6):
        T_b = T_5
        L_b = L_5
        h_b = h_5
    #mesosphere2    
    else:
        T_b = T_6
        L_b = L_6
        h_b = h_6
    
    
    if (L_b == 0):
        T = T_b
    else:
        T = T_b + L_b *(h-h_b)
    
    return T 

 def plot():   
    
     #plot
     
     ##plot altitude 0 to 80 km
     fig = plt.figure()
     ax = plt.axes()
     y = np.linspace(0, 80, 10)
     x = local_atmospheric_temperature(y*1000) - 273.15
     plt.plot(x, y)
     plt.title ("Temperature at Different Altitudes ")
     plt.xlabel ("Temperature in [°C]" )
     plt.ylabel ("Altitude in [km]")
     plt.show()
     return()    
 plot()

the problem occurs at the if clause.
can anybody help?

Please enclose all your Python code in backticks (`) so that the indention is maintained.

2 Likes

This isn’t a Jupyter question. Just basic Python. If you ran this in the Python console or as a traditional script, you’d get the same result.

That being said, to help debugging, insert the following above your problematic if conditional.

print(h_0)
print(h)
print(h_1)

You’ll see h is an array. You need construct a better conditional to test what you want to know.
That error message is well constructed to probably point you to the Python syntax you may need to invoke.