#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    Burlinton, VT
    Posts
    36
    Rep Power
    2

    Too many values to unpack - errorbars


    I am having this annoying error.

    Here my vectors:

    Code:
    >>> VL
    array([ 0.07 ,  0.107,  0.146,  0.186,  0.262,  0.338,  0.414,  0.491,
            0.567,  0.644,  0.72 ,  0.796])
    >>> v1_1
    array([ 31.63965028,  35.51074719,  36.5090816 ,  34.40323434,
            32.75618023,  31.55212687,  30.66315566,  29.65452344,
            28.70684876,  28.02441523,  27.12178116,  26.18805411])
    >>> yerr
    array([ 0.08696274,  0.08374508,  0.0829707 ,  0.08463082,  0.08599984,
            0.08703939,  0.08782748,  0.08874227,  0.08962105,  0.09026498,
            0.09113027,  0.09204065])
    this is what I am using

    Code:
    p1, = plt.errorbar(VL,v1_1,yerr=yerr)
    error:
    Code:
    Traceback (most recent call last):
      File "/home/enrique/Documents/Fluids/lab/Lab 1/lab1.py", line 134, in <module>
        p1, = plt.errorbar(VL,v1_1,yerr=yerr)
    ValueError: too many values to unpack
    all my code

    Code:
    import numpy as np
    import matplotlib.pyplot as plt
    
    # Data reading
    
    filename = '/home/enrique/Documents/Fluids/lab/Lab 1/rawdatalab1.csv'
    
    with open(filename) as f:
        all_the_lines = f.readlines()
    data = []
    for line in all_the_lines:
        data.append([])
        append = data[-1].append
        for word in line.split():
            append(float(word))
    
    data = np.asarray(data) #data into arrays (matlab style)
    [r,c] = np.shape(data)
    
    #Calculate air density
    
    Patm = 754.000/760.000  #atm
    Pair = Patm * 101325  #pascals
    Rspecific = 287.058 #J/kg*k
    Tair = 23.000+273.15  #K
    Adensity = Pair/(Rspecific*Tair)  #Air density in SI units
    
    Wdensity = 1000.000 #water density
    g = 9.81  #gravity
    
    # Create data vectors
    setting1 = np.zeros([r,1])
    setting2 = np.zeros([r,1])
     
    
    for (i,value) in enumerate(setting1):  # Calculate avg data setting 1
        setting1[i] = np.mean(data[i,0:3])/1000
    for (i,value) in enumerate(setting2):  # Average data setting 2
        setting2[i] = np.mean(data[i,4:6])/1000  
    
    ht1 = setting1[16]  #Total pressure height
    ht2 = setting2[16]
    
    # Method 1
    
    v1 = np.zeros([r,1]) #preallocate velocity vector
    v2 = np.zeros([r,1])
    
    # Calculate velocities
    
    for (i,value) in enumerate(setting1):
        v1[i] = np.sqrt((2.0*(Wdensity*g*abs(ht1-value)))/Adensity)
    
    for (i,value) in enumerate(setting2):
        v2[i] = np.sqrt((2.0*(Wdensity*g*abs(ht2-value)))/Adensity)     
    
    # Method 2
    VL = np.zeros([len(v1[3:15]),1])
    TH = np.asarray([9.18,8.33,7.92,8.04,8.33,8.75,9.125,9.42,9.92,10.46,10.985,11.51])*0.0254 # Tunnel section in meters Height
    VL[:,0] = np.asarray([7.0,10.7,14.6,18.6,26.2,33.8,41.4,49.1,56.7,64.4,72.0,79.6])*(1/100.0) # Venturi Location s-direction in meters
    tw = 11.875*0.0254 # tunnel width in meters
    Ai = TH*tw  #Area-Vector
    
    Ainlet = np.power(tw,2) #Area of the inlet
    Vinlet1 = v1[2] #Velocity at the inlet s1
    Vinlet2 = v2[2] #Velocity at the inlet s2
    
    m1 = Adensity*Vinlet1*Ainlet  #constant mass flow rate for setting 1
    m2 = Adensity*Vinlet2*Ainlet  #constant mass flow rate for setting 2
    
    # Calculate Velocities
    
    v1e = m1/(Adensity*Ai)  # v1 for energy methods
    v2e = m2/(Adensity*Ai)  # v2 for energy methods
    
    # Calculations for plots
    
    Pd1 = Wdensity*g*abs(ht1-setting1[3:15])  #Pd for s1 in pa
    Pd2 = Wdensity*g*abs(ht2-setting2[3:15])  #Pd for s2 in pa
    Pt1 = Wdensity*g*ht1   # Pt for s1 (constant)
    Pt2 = Wdensity*g*ht2   # Total pressure for setting 2 (constant)
    Ps1 = Pt1-Pd1  # static pressure s1
    Ps2 = Pt2-Pd2  # static pressure s2
    
    # Uncertainty calculations
    
    Oht = 0.001 #ht uncertainty (m)
    Ohs = 0.001 #hs uncertainty (m)
    Ot = 0.1 # Temperature uncertainty (K)
    Op = 3.3322368 # Uncertainty for Atmospheric pressure
    
    
    term1 = np.power(Oht,2)
    term2 = np.power((0.5*np.sqrt(2*Rspecific*Tair*Wdensity*g/Pair)*float(1/np.sqrt(ht1))),2)
    term3 = np.power(Ohs,2)
    term4 = np.power((0.5*np.sqrt(2*Rspecific*Tair*Wdensity*g/Pair)*(1/np.sqrt(setting1))),2)
    term5 = np.power(Op,2)
    term6 = np.power((0.5*np.sqrt(2*Rspecific*Tair*Wdensity*g*abs((ht1 - setting1)))*np.power((1/Pair),1.5)),2) 
    term7 = np.power(Ot,2)
    term8 = np.power((0.5*np.sqrt(2*Rspecific*Wdensity*g*abs((ht1 - setting1))/Pair)*1/np.sqrt(Tair)),2)
    
    Ov1= term1*term2+term3*term4+term5*term6+term7*term8
    
    
    
    # Plots
    
    # Plot # 1
    plt.figure(1)
    plt.subplot(2,1,1)
    p1, = plt.plot(VL,Ps1)
    p2, = plt.plot(VL,Pd1)
    plt.ylabel('Static and Dynamic Pressures (kpa)')
    plt.xlabel('S-direction (m)')
    p3, = plt.plot(VL,Ps2)
    p4, = plt.plot(VL,Pd2)
    plt.legend([p1, p2, p3, p4], ["Ps for s1", "Pd for s1", "Ps for s2", "Pd for s2"])
    plt.axis([0.0,1,-1700,2200])
    plt.subplot(2,1,2)
    p5, = plt.plot(VL,Ai)
    plt.axis([0.0,1,0.060,0.090])
    plt.ylabel('Ai(m^2)')
    plt.xlabel('S-direction (m)')
    plt.title('Area distribution')
    
    # Plot # 2
    v1_1 = v1[3:15]
    yerr = Ov1[3:15]
    VL = np.reshape(VL, (VL.shape[0], ))
    v1_1 = np.reshape(v1_1, (v1_1.shape[0], ))
    yerr = np.reshape(yerr, (yerr.shape[0], ))
    plt.figure(2)
    plt.subplot(2,1,1)
    p1, = plt.errorbar(VL,v1_1,yerr=yerr)
    p2, = plt.errorbar(VL,v2[3:15])
    plt.ylabel('Velocity (m/s)')
    plt.xlabel('S-direction (m)')
    p3, = plt.errorbar(VL,v1e)
    p4, = plt.errobar(VL,v2e)
    plt.legend([p1, p2, p3, p4], ["v1 bernoulli", "v2 bernoulli", "v1 energy", "v2 energy"])
    plt.subplot(2,1,2)
    p5, = plt.errorbar(VL,Ai)
    plt.ylabel('Ai(m^2)')
    plt.xlabel('S-direction (m)')
    plt.title('Area distribution')
    
    # Relevant data saving
    v = np.zeros([20,2])
    v[:,0] = v1[:,0]
    v[:,1] = v2[:,0]
    np.savetxt('velocities.out',v,fmt='%.4f',delimiter=',')
    ve = np.zeros([len(v1e),2])
    ve[:,0] = v1e
    ve[:,1] = v2e
    np.savetxt('velocities_e.out',ve,fmt='%.4f',delimiter=',')
    raw data

    Code:
    75	75	75	70	70	71
    75	74	75	70	70	71
    118	118	119	176	177	178
    135	134	134	219	221	221
    150	150	150	257	257	260
    155	154	154	266	268	270
    145	145	146	248	250	250
    138	139	139	231	233	234
    134	134	134	219	220	221
    130	131	131	208	210	210
    126	127	128	200	201	202
    123	124	124	192	193	194
    121	121	122	185	187	186
    118	118	119	178	179	179
    115	115	116	169	170	169
    129	129	130	207	208	209
    74	74	74	70	71	71
    74	74	74	69	69	69
    74	74	74	69	69	69
    74	74	74	69	69	69
    Thanks you for your help
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    Where you have

    p1, =

    is a tuple assignment. (p1,) represents a tuple of 1 item.
    the errorbar method seems to return an iterable length 3. Three isn't one, and that's why python complains about too many items to unpack. You could use

    (a,b,c,) = plt.errorbar(args)

    but it isn't what you want, I think, although I don't have a lot of experience with matplotlib. I think you want to keep the <Container object of 3 artists> intact, thus you really want

    p1 = plt.errorbar...

    Fixing these mistakes, and correcting your errobar spelling mistake gives this program which runs cleanly, displaying some plots. I didn't check the science.
    Code:
    import numpy as np
    import matplotlib.pyplot as plt
    
    # Data reading
    
    filename = '/home/enrique/Documents/Fluids/lab/Lab 1/rawdatalab1.csv' or 'a'
    
    with open(filename) as f:
        all_the_lines = f.readlines()
    data = []
    for line in all_the_lines:
        data.append([])
        append = data[-1].append
        for word in line.split():
            append(float(word))
    
    data = np.asarray(data) #data into arrays (matlab style)
    [r,c] = np.shape(data)
    
    #Calculate air density
    
    Patm = 754.000/760.000  #atm
    Pair = Patm * 101325  #pascals
    Rspecific = 287.058 #J/kg*k
    Tair = 23.000+273.15  #K
    Adensity = Pair/(Rspecific*Tair)  #Air density in SI units
    
    Wdensity = 1000.000 #water density
    g = 9.81  #gravity
    
    # Create data vectors
    setting1 = np.zeros([r,1])
    setting2 = np.zeros([r,1])
     
    
    for (i,value) in enumerate(setting1):  # Calculate avg data setting 1
        setting1[i] = np.mean(data[i,0:3])/1000
    for (i,value) in enumerate(setting2):  # Average data setting 2
        setting2[i] = np.mean(data[i,4:6])/1000  
    
    ht1 = setting1[16]  #Total pressure height
    ht2 = setting2[16]
    
    # Method 1
    
    v1 = np.zeros([r,1]) #preallocate velocity vector
    v2 = np.zeros([r,1])
    
    # Calculate velocities
    
    for (i,value) in enumerate(setting1):
        v1[i] = np.sqrt((2.0*(Wdensity*g*abs(ht1-value)))/Adensity)
    
    for (i,value) in enumerate(setting2):
        v2[i] = np.sqrt((2.0*(Wdensity*g*abs(ht2-value)))/Adensity)     
    
    # Method 2
    VL = np.zeros([len(v1[3:15]),1])
    TH = np.asarray([9.18,8.33,7.92,8.04,8.33,8.75,9.125,9.42,9.92,10.46,10.985,11.51])*0.0254 # Tunnel section in meters Height
    VL[:,0] = np.asarray([7.0,10.7,14.6,18.6,26.2,33.8,41.4,49.1,56.7,64.4,72.0,79.6])*(1/100.0) # Venturi Location s-direction in meters
    tw = 11.875*0.0254 # tunnel width in meters
    Ai = TH*tw  #Area-Vector
    
    Ainlet = np.power(tw,2) #Area of the inlet
    Vinlet1 = v1[2] #Velocity at the inlet s1
    Vinlet2 = v2[2] #Velocity at the inlet s2
    
    m1 = Adensity*Vinlet1*Ainlet  #constant mass flow rate for setting 1
    m2 = Adensity*Vinlet2*Ainlet  #constant mass flow rate for setting 2
    
    # Calculate Velocities
    
    v1e = m1/(Adensity*Ai)  # v1 for energy methods
    v2e = m2/(Adensity*Ai)  # v2 for energy methods
    
    # Calculations for plots
    
    Pd1 = Wdensity*g*abs(ht1-setting1[3:15])  #Pd for s1 in pa
    Pd2 = Wdensity*g*abs(ht2-setting2[3:15])  #Pd for s2 in pa
    Pt1 = Wdensity*g*ht1   # Pt for s1 (constant)
    Pt2 = Wdensity*g*ht2   # Total pressure for setting 2 (constant)
    Ps1 = Pt1-Pd1  # static pressure s1
    Ps2 = Pt2-Pd2  # static pressure s2
    
    # Uncertainty calculations
    
    Oht = 0.001 #ht uncertainty (m)
    Ohs = 0.001 #hs uncertainty (m)
    Ot = 0.1 # Temperature uncertainty (K)
    Op = 3.3322368 # Uncertainty for Atmospheric pressure
    
    
    term1 = np.power(Oht,2)
    term2 = np.power((0.5*np.sqrt(2*Rspecific*Tair*Wdensity*g/Pair)*float(1/np.sqrt(ht1))),2)
    term3 = np.power(Ohs,2)
    term4 = np.power((0.5*np.sqrt(2*Rspecific*Tair*Wdensity*g/Pair)*(1/np.sqrt(setting1))),2)
    term5 = np.power(Op,2)
    term6 = np.power((0.5*np.sqrt(2*Rspecific*Tair*Wdensity*g*abs((ht1 - setting1)))*np.power((1/Pair),1.5)),2) 
    term7 = np.power(Ot,2)
    term8 = np.power((0.5*np.sqrt(2*Rspecific*Wdensity*g*abs((ht1 - setting1))/Pair)*1/np.sqrt(Tair)),2)
    
    Ov1= term1*term2+term3*term4+term5*term6+term7*term8
    
    
    
    # Plots
    
    # Plot # 1
    plt.figure(1)
    plt.subplot(2,1,1)
    p1, = plt.plot(VL,Ps1)
    p2, = plt.plot(VL,Pd1)
    plt.ylabel('Static and Dynamic Pressures (kpa)')
    plt.xlabel('S-direction (m)')
    p3, = plt.plot(VL,Ps2)
    p4, = plt.plot(VL,Pd2)
    plt.legend([p1, p2, p3, p4], ["Ps for s1", "Pd for s1", "Ps for s2", "Pd for s2"])
    plt.axis([0.0,1,-1700,2200])
    plt.subplot(2,1,2)
    p5, = plt.plot(VL,Ai)
    plt.axis([0.0,1,0.060,0.090])
    plt.ylabel('Ai(m^2)')
    plt.xlabel('S-direction (m)')
    plt.title('Area distribution')
    
    # Plot # 2
    v1_1 = v1[3:15]
    yerr = Ov1[3:15]
    VL = np.reshape(VL, (VL.shape[0], ))
    v1_1 = np.reshape(v1_1, (v1_1.shape[0], ))
    yerr = np.reshape(yerr, (yerr.shape[0], ))
    plt.figure(2)
    plt.subplot(2,1,1)
    p1 = plt.errorbar(VL,v1_1,yerr=yerr)
    p2 = plt.errorbar(VL,v2[3:15])
    plt.ylabel('Velocity (m/s)')
    plt.xlabel('S-direction (m)')
    p3 = plt.errorbar(VL,v1e)
    p4 = plt.errorbar(VL,v2e)
    plt.legend([p1, p2, p3, p4], ["v1 bernoulli", "v2 bernoulli", "v1 energy", "v2 energy"])
    plt.subplot(2,1,2)
    p5 = plt.errorbar(VL,Ai)
    plt.ylabel('Ai(m^2)')
    plt.xlabel('S-direction (m)')
    plt.title('Area distribution')
    
    # Relevant data saving
    v = np.zeros([20,2])
    v[:,0] = v1[:,0]
    v[:,1] = v2[:,0]
    np.savetxt('velocities.out',v,fmt='%.4f',delimiter=',')
    ve = np.zeros([len(v1e),2])
    ve[:,0] = v1e
    ve[:,1] = v2e
    np.savetxt('velocities_e.out',ve,fmt='%.4f',delimiter=',')

    Comments on this post

    • eangola agrees : Great answer
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    Burlinton, VT
    Posts
    36
    Rep Power
    2
    Thank you,

    That fixed the problem. I look into everywhere but that, that's why I tried reshaping the vectors and everything. Makes a lot of sense now.

IMN logo majestic logo threadwatch logo seochat tools logo