我有数据集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()
有什么建议吗?
答案 0 :(得分:2)
您可以使用x1
或x2
条件索引y==0
和y==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()