Python我的散点图不起作用

时间:2015-11-18 20:02:29

标签: python python-2.7 matplotlib scatter-plot

我正在绘制浮点matplotlib数组的散点图(使用numpy)。但是我收到了错误:

  

float()参数必须是字符串或数字。

以下是我的完整代码。我想用两种不同的颜色绘制两个系列。

mu = 0
sigma1 = 1 # mean and standard deviation
x1_1 = np.random.normal(mu, sigma1, 10000)
x1_1 = np.sort(x1_1)

x2_1 = np.random.normal(mu,sigma1,10000)
x2_1 = np.sort(x2_1)

sigma2 = 4 # mean and standard deviation
x1_2 = np.random.normal(mu, sigma2, 10000)
x1_2 = np.sort(x1_2)

x2_2 = np.random.normal(mu, sigma2, 10000)
x2_2 = np.sort(x2_2)

x1 = []
x2 = []
y = 0
for i in xrange(0,10000):
  if y == 0:
    x1.append(x1_1[i])
    x2.append(x2_1[i])
    y = 1
  else:
    x1.append(x1_2[i])
    x2.append(x2_2[i])
    y = 0


x1_np = np.asarray(x1)
x2_np = np.asarray(x2)

print len(x1_np)
print len(x2_np)

x1_train = x1_np[0:500]
x2_train = x2_np[0:500]


x1_train_0 = set(x1_train).intersection(set(x1_1))
x1_train_1 = set(x1_train).intersection(set(x1_2))

x2_train_0 = set(x2_train).intersection(set(x2_1))
x2_train_1 = set(x2_train).intersection(set(x2_2))

print len(x1_train_0)
print len(x1_train_1)
print len(x2_train_0)
print len(x2_train_1)

x1_train_0 = np.asarray(x1_train_0)
x1_train_1 = np.asarray(x1_train_1)

x2_train_0 = np.asarray(x2_train_0)
x2_train_1 = np.asarray(x2_train_1)

plt.scatter(x1_train_0,x2_train_0,c = 'red')
plt.scatter(x1_train_1,x2_train_1,c = 'blue')
plt.show()

2 个答案:

答案 0 :(得分:0)

要将python集转换为numpy数组,首先应将其转换为python列表

x1_train_0 = np.asarray(list(x1_train_0))
x1_train_1 = np.asarray(list(x1_train_1))

x2_train_0 = np.asarray(list(x2_train_0))
x2_train_1 = np.asarray(list(x2_train_1))

答案 1 :(得分:0)

你可以写完

x1_train_0 = np.asarray(x1_train_0)
x1_train_1 = np.asarray(x1_train_1)

x2_train_0 = np.asarray(x2_train_0)
x2_train_1 = np.asarray(x2_train_1)

这些行:

x1_train_0 = list(x1_train_0.all())
x1_train_1 = list(x1_train_1.all())

x2_train_0 = list(x2_train_0.all())
x2_train_1 = list(x2_train_1.all())

将numpy.ndarray转换为列表。