用不同的标记python绘制类

时间:2016-08-26 15:28:24

标签: numpy matplotlib scatter-plot

我有数据集x1和x2以及y类的值,其值为0或1.我想在散点图中绘制x1和x2,使得值y == 1将显示为" +& #34;和值y == 0将显示为" o"。

x1 = np.array(100)
x2 = np.array(100)
#y = array of length 100 either with value 1 or 0
plt.scatter(x1, x2, y=1, marker='+')
plt.scatter(x1, x2, y=0, marker='o')
plt.show()

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

您可以使用x1x2条件索引y==0y==1数组:

plt.scatter(x1[y==1], x2[y==1], marker='+')
plt.scatter(x1[y==0], x2[y==0], marker='o')

答案 1 :(得分:1)

使用np.where获取y数组为0或1的索引,然后相应地绘制它们。以下是一个例子

import matplotlib.pyplot as plt
import numpy as np

plt.close('all')


x = np.arange(100)
y = np.random.randint(0, 2, 100)

arg_0 = np.where(y == 0)
arg_1 = np.where(y == 1)

fig, ax = plt.subplots()
ax.scatter(x[arg_0], y[arg_0], marker='o')
ax.scatter(x[arg_1], y[arg_1], marker='+')
ax.set_ylim(-0.1, 1.1)
fig.show()