如何在 python 的散点函数的一个参数中设置颜色和标记

How can I set color and marker in one parameter for scatter function in python

我尝试根据标签对一系列数据使用不同的颜色和标记:

我写了:

   for x,l in zip(X,labels):
        ax.scatter(x[0],x[1], 'xb' if l == -1 else 'or')

by 'xb' 我的意思是 'x' 用于标记,'b' 用于颜色,但它不起作用。

所以我写成:

for x,l in zip(X,labels):
    ax.scatter(x[0],x[1], marker='x' if l == -1 else 'o', c='r' if l == -1 else 'b')

不使用两个单独命名参数的正确参数是什么?或者任何其他缩短语句的技巧。

到目前为止,我还没有在 plot 函数中看到 if-else 结构。我会更改标签列表:

X = [(1, 4), (2, 6), (3, 2), (4, 8), (5, 3), (6,1)]
labels = [-1, 0, 0, -1, 0, -1]

fig, ax = plt.subplots()
labels = ["xb" if item == -1 else "or" for item in labels]

for x,l in zip(X, labels):
    ax.plot(x[0],x[1], l)

plt.show()

还值得注意的是,散点图和绘图使用不同的标记定义约定。 plot 的快捷方式不适用于 scatter,其中大小 s、颜色 c 和标记样式 m 必须单独提供。