Numpy 使用不同的标记绘制类

Numpy 使用不同的标记绘制类,numpy,matplotlib,scatter-plot,Numpy,Matplotlib,Scatter Plot,我有数据集列的特征x1和x2以及y类,其值为0或1。我想在散点图中绘制x1和x2,使值y==1显示为“+”,值y==0显示为“o” 有什么建议吗?使用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.w

我有数据集列的特征x1和x2以及y类,其值为0或1。我想在散点图中绘制x1和x2,使值y==1显示为“+”,值y==0显示为“o”


有什么建议吗?

使用
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()

您可以使用
y==0
y==1
的条件对
x1
x2
数组进行索引:

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