简体   繁体   中英

ValueError: x and y must have same first dimension, but have shapes (41,) and (1, 41)

I'm trying to plot a vs kappa_inv and I keep getting the error: ValueError: x and y must have same first dimension, but have shapes (41,) and (1, 41).

I saw a prev post about changing plt.plot square brackets to round ones but the error is still occurring. Can anyone see what I'm doing wrong?

import numpy

L = [20,20, 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20]
L = numpy.array(L)
delta = [0.5, 0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5]
delta = numpy.array(delta)
x = L/delta

a =[-0.5,0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5,6,6.5,7,7.5,8,8.5,9,9.5,10,10.5,11,11.5,12,12.5,13,13.5,14,14.5,15,15.5,16,16.5,17,17.5,18,18.5,19,19.5]
numpy.array(a)
#Force
F = 100 #kN

#calc sigma
y = 250 #mm
E = 32800 #MPa
I =1.837E9 #mm4
sig = y/(E*I)
print (sig)

kappa = []
b = []
y = 20
while y >= 0: 
   b.append(y)
   y = y-0.5
   numpy.array(b)
for val in a:
    val = "{:.1f}".format(val)
    val = float(val)
    fraction = b/L

kappa_i = fraction * val

kappa.append(kappa_i)
b = b - delta

N = 4
Length = len(kappa)
pad_kappa = numpy.pad(kappa,(0,N),'constant', constant_values = 0)
print(pad_kappa)

#Calc bending moment list
BM = []
for k in range (0,Length):
    bendingMoment = (pad_kappa[k]*F) + (pad_kappa[k+3]*F)
    BM.append(bendingMoment)
    print(BM)

Strain =[]
for j in range(0,len(BM)):
    strain = (BM[j] * sig) * 10E6
    Strain.append(strain)

kappa_inv = [ -x for x in kappa]
numpy.array(kappa_inv)

import matplotlib.pyplot as plt
plt.plot(a,kappa_inv)
plt.ylabel('KAPPA')
plt.xlabel('LENGTH ALONG BEAM')
plt.show()

#E = BM*10E6 * sigma
strainCalcReverse = []
for s in Strain:
    bendYourMomLOL = s/sig * (1/10E6)
    bendYourMomLOL.append(strainCalcReverse)

print(strainCalcReverse)

There's a lot of messy stuff in your code,

Lines like:

numpy.array(a)          # doesn't change list a
numpy.array(b)          # same

but

fraction = b/L          # only works if b is an array, not a list.

Looks like this is trying to turn all elements in a to float, but that's not how a python loop works.

for val in a:
    val = "{:.1f}".format(val)
    val = float(val)

a = np.array(a) will produce a float array, so there's no need for this loop.

Anyways, it looks like kappa_i is an array. If so then the following demonstrates your error:

In [311]: kappa=[]
In [312]: kappa.append(np.arange(3))
In [313]: kappa
Out[313]: [array([0, 1, 2])]
In [314]: plt.plot([1,2,3], kappa)
Traceback (most recent call last):
  File "<ipython-input-314-e15645b5613f>", line 1, in <module>
    plt.plot([1,2,3], kappa)
  File "/usr/local/lib/python3.8/dist-packages/matplotlib/pyplot.py", line 2988, in plot
    return gca().plot(
  File "/usr/local/lib/python3.8/dist-packages/matplotlib/axes/_axes.py", line 1605, in plot
    lines = [*self._get_lines(*args, data=data, **kwargs)]
  File "/usr/local/lib/python3.8/dist-packages/matplotlib/axes/_base.py", line 315, in __call__
    yield from self._plot_args(this, kwargs)
  File "/usr/local/lib/python3.8/dist-packages/matplotlib/axes/_base.py", line 501, in _plot_args
    raise ValueError(f"x and y must have same first dimension, but "
ValueError: x and y must have same first dimension, but have shapes (3,) and (1, 3)

By using that list append, you made a list with one array element. When passed to plot that is produces a (1,n) array.

Correct your code, whether it's the actual code or the copy to the question. And pay closer attention to when variables are lists, or arrays, and if arrays, what's the shape and dtype .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM