ValueError: x and and must have same first dimension

1

Greetings:

I'm pretty new using Python and I was practicing with the following problem:

I wanted to graph the value of heat transfer through an aluminum plate (I tried to make it as simple as possible by simply practicing how to graph results in python).

import matplotlib.pyplot as plt


span_L = []                           #Initialize span
span_Q = []
Q= []
Delta_T = 100                        #Assuming delta T = 100 C
T_cond = (207 /100)                  #Aluminum thermal conductivity W/(cm K)
Area = 1                             #Assuming Area is 1 cm^2


for lenght in range (100):         #Loop that appends different values
  span_L.append(float(lenght))

print("Lenght (L) in centimeters: ", span_L)     #Shows the output of values

for span_L in range (1,101):
  Q =  (T_cond*Area*Delta_T/span_L)
  span_Q.append(float(Q))

print(span_Q)

plt.plot(span_L,span_Q)
plt.title("Heat transfer for different thichkness values in an Al plate")
plt.xlabel("Lenght in cm")
plt.ylabel("Heat transfer in Watts [W]")
plt.show()

The problem I have is that I get the error: ValueError: x and y must have same first dimension and when checking the length of both lists, both have the same length.

    
asked by Eli Samuel González Arroyo 11.01.2017 в 01:20
source

1 answer

1

The problem is in the second cycle for , when you do this:

for span_L in range (1,101):
    ...

The list% co_of% that you have created before reassigning it as an integer whose value varies during the cycle, remaining with a final value of 100. Therefore, when you try to graph span_L vs span_L send you just that error.

To solve this, you can use an auxiliary variable in the cycle span_Q , something like:

import matplotlib.pyplot as plt

span_L = []                           #Initialize span
span_Q = []
Q= []
Delta_T = 100                        #Assuming delta T = 100 C
T_cond = (207 /100)                  #Aluminum thermal conductivity W/(cm K)
Area = 1                             #Assuming Area is 1 cm^2

for lenght in range (100):         #Loop that appends different values
  span_L.append(float(lenght))

print("Length (L) in centimeters: ", span_L)     #Shows the output of values

for _span_L in range (1,101):
  Q =  (T_cond*Area*Delta_T/_span_L)
  span_Q.append(float(Q))

plt.plot(span_L,span_Q)
plt.title("Heat transfer for different thickness values in an Al plate")
plt.xlabel("Length in cm")
plt.ylabel("Heat transfer in Watts [W]")
plt.show()

Although, from what I understand, I think you could save yourself the first loop and some initial definitions of lists like Q or span_L.

import matplotlib.pyplot as plt

span_Q = []                           #Initialize span
Delta_T = 100                        #Assuming delta T = 100 C
T_cond = (207 /100)                  #Aluminum thermal conductivity W/(cm K)
Area = 1                             #Assuming Area is 1 cm^2

span_L = range(1,101)

print("Length (L) in centimeters: ", span_L)     #Shows the output of values

for L in span_L:
  Q =  (T_cond*Area*Delta_T/L)
  span_Q.append(float(Q))

plt.plot(span_L,span_Q)
plt.title("Heat transfer for different thickness values in an Al plate")
plt.xlabel("Length in cm")
plt.ylabel("Heat transfer in Watts [W]")
plt.show()

Now, as a recommendation, I would strongly suggest using NumPy to define vectors with which you can work more comfortably and efficiently, for example for you, the code would be reduced to:

import matplotlib.pyplot as plt
import numpy as np

Delta_T = 100                        #Assuming delta T = 100 C
T_cond = (207/100)                  #Aluminum thermal conductivity W/(cm K)
Area = 1                             #Assuming Area is 1 cm^2

span_L = np.linspace(1,100)
span_Q = (T_cond*Area*Delta_T/span_L)

plt.plot(span_L,span_Q)
plt.title("Heat transfer for different thickness values in an Al plate")
plt.xlabel("Length in cm")
plt.ylabel("Heat transfer in Watts [W]")
plt.show()

You can find more information about working with Matplotlib + Numpy at Scipy Lecture Notes .

    
answered by 11.01.2017 в 03:38