ValueError: x和y必须是相同大小的Python。

时间:2022-12-28 16:13:41

If I want to make a scatter plot with matplotlib like this:

如果我想用matplotlib做一个散点图:

import matplotlib as plt
x = [float(1) for x in xrange(2)]
y = [float(2) for x in xrange(2)]
plt.scatter(x,y)
plt.show()

I always get the above Error. But if I do as so instead:

我总是得到上面的错误。但如果我这样做,

import matplotlib as plt
x = [1.0, 1.0]
y = [2.0, 2.0]
plt.scatter(x,y)
plt.show()

it works. Why is that?

它的工作原理。这是为什么呢?

1 个解决方案

#1


4  

You are overwriting x when assigning y

在分配y时,你重写了x。

x = [float(1) for x in xrange(2)] # x = [1, 1]
y = [float(2) for x in xrange(2)] # x = 1; y = [2, 2]
                  ^

Instead of using x use _ (the "don't care variable in python" as suggested by @kroolik)

而不是使用x使用_(如@kroolik所建议的“不关心python中的变量”)

x = [float(1) for _ in xrange(2)]
y = [float(2) for _ in xrange(2)]

#1


4  

You are overwriting x when assigning y

在分配y时,你重写了x。

x = [float(1) for x in xrange(2)] # x = [1, 1]
y = [float(2) for x in xrange(2)] # x = 1; y = [2, 2]
                  ^

Instead of using x use _ (the "don't care variable in python" as suggested by @kroolik)

而不是使用x使用_(如@kroolik所建议的“不关心python中的变量”)

x = [float(1) for _ in xrange(2)]
y = [float(2) for _ in xrange(2)]