简体   繁体   English

ValueError: x 和 y 必须具有相同的第一维异常被抛出,但 x 和 y 的类型和长度相同

[英]ValueError: x and y must have same first dimension exception is thrown, but x and y are of the same type and length

So while i'm trying to figure out how the get the mean average of an numpy array and to plot it.因此,当我试图弄清楚如何获得 numpy 数组的平均平均值并绘制它时。 I got the following error message:我收到以下错误消息:

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

My code is as follows:我的代码如下:

t = np.arange(0,100, 10)
x = np.arange(10)

print type(t), type(x), len(t), len(x), t, x


average = np.array([])
for x in range(len(t)):
    mask = np.ones(len(t), dtype=bool)
    if x is not 0:
        mask[x-1] = False
    mask[x]= False
    if x+1 is not len(t):
        mask[x+1]= False
    b = np.ma.array(t,mask=mask)
    average = np.append(average, np.ma.average(b))


plt.plot(x, t)
plt.plot(x, average)
plt.show'

the print returns the following打印返回以下内容

<type 'numpy.ndarray'> <type 'numpy.ndarray'> 10 10 [ 0 10 20 30 40 50 60 70 80 90] [0 1 2 3 4 5 6 7 8 9]

but then at the plots it throws the error.但是然后在绘图中它会引发错误。 I don't understand why because x and t are of the same length and type.我不明白为什么,因为 x 和 t 的长度和类型相同。

I even tried to reproduce it but then it suddenly works:我什至试图重现它,但它突然起作用了:

f = np.arange(10)
g = np.arange(0,100, 10)
print f, g
plt.plot(f, g)

[0 1 2 3 4 5 6 7 8 9] [ 0 10 20 30 40 50 60 70 80 90] [0 1 2 3 4 5 6 7 8 9] [ 0 10 20 30 40 50 60 70 80 90]

在此处输入图片说明

Can anybody tell me why it doesn't work?谁能告诉我为什么它不起作用? and why it does work when I try to reproduce it?为什么当我尝试重现它时它会起作用?

The name of your list x gets overwritten by the x in your for loop.您的列表x的名称被 for 循环中的x覆盖。 Change it to for i in range and it will work, or alternatively change the name of your list:将其更改为for i in range它将起作用,或者更改您的列表名称:

t = np.arange(0,100, 10)
x = np.arange(10)

average = np.array([])
for i in range(len(t)):
    mask = np.ones(len(t), dtype=bool)
    if i is not 0:
        mask[i-1] = False
    mask[i]= False
    if i+1 is not len(t):
        mask[i+1]= False
    b = np.ma.array(t,mask=mask)
    average = np.append(average, np.ma.average(b))

plt.plot(x, t)
plt.plot(x, average)

plt.show()

在此处输入图片说明

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

相关问题 输入错误,然后输入值错误:x 和 y 必须具有相同的第一维 - Type error, and then ValueError: x and y must have same first dimension 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,) Python ValueError:x 和 y 必须具有相同的第一维 - Python ValueError: x and y must have same first dimension Matplotlib:ValueError:x和y必须具有相同的第一维错误 - Matplotlib: ValueError: x and y must have same first dimension Error 绘图:ValueError:x 和 y 必须具有相同的第一维 - Plotting: ValueError: x and y must have same first dimension Python,ValueError:x 和 y 必须具有相同的第一维问题 - Python, ValueError: x and y must have same first dimension issue “ ValueError:x和y必须具有相同的第一维”的不同情况 - A different situation of “ValueError: x and y must have same first dimension” ValueError:绘制时x和y的第一维必须相同 - ValueError: x and y must have same first dimension when plotting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM