首页 > 解决方案 > Python Scatter Plot Edgecolors by Label

问题描述

我试图弄清楚如何动态设置数据点的边缘颜色。可以使用 c=[数字标签数组] 设置 facecolor,但对于 edgecolors 则不然。

我试图实现的是一个图,其中 facecolor 等于实例算法的预测标签,而 edgecolor 等于该实例的真实标签。这样,我可以直观地比较结果(对于进一步的工作很重要)。

有什么建议么?到目前为止,这是我的代码:

fig, axs = plt.subplots(2, 2)
axs[0, 0].scatter(X_train[:,0], X_train[:,1], c=y_train)
axs[0, 0].set_title('org.Data with true labels')
axs[0, 1].scatter(X_train[:,0], X_train[:,1], c=y_pred)
axs[0, 1].set_title('org.Data with LOF labels')
axs[1, 0].scatter(data_PCA[:,0],data_PCA[:,1], c=y_pred)
axs[1, 0].set_title("PCAplt, labels LOF: org.Data")
axs[1, 1].scatter(data_PCA[:,0],data_PCA[:,1], c=y_pred_PCA)
axs[1, 1].set_title('PCA.plt, labels LOF: PCA')

这给了我以下情节

在此处输入图像描述

我很感激任何建议。谢谢!

标签: pythonmatplotlibscatter

解决方案


有时,在放置东西休息一段时间后,答案就会出现。我找到了一个解决方案并想分享以防万一有人需要这个:

简而言之:我使用非常简单的列表理解预先计算了一个“真实标签颜色”列表,并使用了另一个长度为 2 的列表,颜色相同。

编码:

colors = ["green","red"]
true_labels_color_encoded = ["red" if label == 1 else "green" for label in y_train]

fig, axs = plt.subplots(2, 2)
axs[0, 0].scatter(X_train[:,0], X_train[:,1], c=y_train, cmap=ListedColormap(colors), edgecolors=true_labels_color_encoded)
axs[0, 0].set_title('org.Data with true labels')
axs[0, 1].scatter(X_train[:,0], X_train[:,1], c=y_pred,cmap=ListedColormap(colors),edgecolors=true_labels_color_encoded)
axs[0, 1].set_title('org.Data with LOF labels')
axs[1, 0].scatter(data_PCA[:,0],data_PCA[:,1], c=y_pred,cmap=ListedColormap(colors),edgecolors=true_labels_color_encoded)
axs[1, 0].set_title("PCAplt, labels LOF: org.Data")
axs[1, 1].scatter(data_PCA[:,0],data_PCA[:,1], c=y_pred_PCA,cmap=ListedColormap(colors),edgecolors=true_labels_color_encoded)
axs[1, 1].set_title('PCA.plt, labels LOF: PCA')

所以 true_labels_color_encoded 看起来像 = ["red","green","green","green"] (注意:在我的例子中,y_train = 1 中的标签表示异常,0 表示正常)

这给了我以下情节:

在这里查看我的情节

使用 ISOMAP 或 t-SNE 等其他缩减技术我得到了更好的结果。


推荐阅读