使用不同的标记 python 绘制 class

Plotting class with different marker python

我有特征 x1 和 x2 和 class y 的数据集列,其值为 0 或 1。我想在散点图中绘制 x1 和 x2,这样值 y == 1 将显示为“+”和值 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()

有什么建议吗?

使用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==0y==1:

的条件索引您的 x1x2 数组
plt.scatter(x1[y==1], x2[y==1], marker='+')
plt.scatter(x1[y==0], x2[y==0], marker='o')