简体   繁体   English

ValueError: x 和 y 必须具有相同的第一维,但具有形状 (41,) 和 (1, 41)

[英]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).我正在尝试 plot a vs kappa_inv 并且不断收到错误消息:ValueError:x 和 y 必须具有相同的第一维,但具有形状 (41,) 和 (1, 41)。

I saw a prev post about changing plt.plot square brackets to round ones but the error is still occurring.我看到一篇关于将 plt.plot 方括号更改为圆括号的上一篇文章,但错误仍然存在。 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.看起来这是试图将a中的所有元素变为浮动,但这不是 python 循环的工作方式。

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. a = np.array(a)将产生一个浮点数组,因此不需要这个循环。

Anyways, it looks like kappa_i is an array.无论如何,看起来kappa_i是一个数组。 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.通过使用该列表 append,您创建了一个包含一个数组元素的列表。 When passed to plot that is produces a (1,n) array.当传递给 plot 时,它会产生一个 (1,n) 数组。

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 .并密切注意变量何时是列表,或 arrays,如果 arrays, shapedtype是什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:x 和 y 必须具有相同的第一维,但具有形状 (1, 2) 和 (2,) - ValueError: x and y must have same first dimension, but have shapes (1, 2) and (2,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 - ValueError: x and y must have same first dimension, but have shapes ValueError:x 和 y 必须具有相同的第一维,但具有形状 (6,) 和 (8,) - ValueError: x and y must have same first dimension, but have shapes (6,) and (8,) 线性回归模型形状 - ValueError:x 和 y 必须具有相同的第一维,但具有形状 (5,) 和 (1, 5) - Linear regression model shapes - ValueError: x and y must have same first dimension, but have shapes (5,) and (1, 5) ValueError:x和y必须具有相同的第一尺寸,但形状为(4200,)和(16800,1) - ValueError: x and y must have same first dimension, but have shapes (4200,) and (16800, 1) ValueError:x 和 y 必须具有相同的第一维,但具有形状 (10000,) 和 (1, 10000) - ValueError: x and y must have same first dimension, but have shapes (10000,) and (1, 10000) ValueError: x 和 y 必须具有相同的第一维,但具有形状 (10, 1) 和 (90,) - ValueError: x and y must have same first dimension, but have shapes (10, 1) and (90,) 线性回归:ValueError:x 和 y 必须具有相同的第一维,但具有形状 (10, 1) 和 (1, 1) - Linear Regression : ValueError: x and y must have same first dimension, but have shapes (10, 1) and (1, 1) ValueError:x 和 y 必须具有相同的第一维,但具有形状 (165,) 和 (166,) - ValueError: x and y must have same first dimension, but have shapes (165,) and (166,) Matplotlib 中的 Plot K-Means:ValueError:x 和 y 必须具有相同的第一个维度,但具有形状 (10,) 和 (1,) - Plot K-Means in Matplotlib: ValueError: x and y must have same first dimension, but have shapes (10,) and (1,)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM