简体   繁体   English

如何将图例 colors 与 plot colors 匹配?

[英]how can I match legend colors with plot colors?

I'm working with a pretty simple example.我正在使用一个非常简单的示例。 I create three scatter plots on the same set of axes, and each data set I plot has a different associated colormap.我在同一组轴上创建了三个散点图,每个数据集 I plot 具有不同的关联颜色图。 However, the legend does not look as I'd want it to;然而,这个传说看起来并不像我想要的那样。 why is this?为什么是这样?

import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs

X, y = make_blobs(n_samples=500, n_features=2, cluster_std=1.0, centers=[(0,0), (3,3)])

plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='rainbow');


clf_tree = DecisionTreeClassifier(criterion='entropy', max_depth=1)
clf_tree = clf_tree.fit(X, y)
y_pred = clf_tree.predict(X)

clf_tree = DecisionTreeClassifier(criterion='entropy', min_samples_leaf = 3)
clf_tree = clf_tree.fit(X, y)
y_pred = clf_tree.predict(X)

clf_tree = DecisionTreeClassifier(criterion='entropy', max_leaf_nodes = 3)
clf_tree = clf_tree.fit(X, y)
y_pred = clf_tree.predict(X)



#shap.decision_plot(expected_value, sh, features_display, link='logit', highlight=misclassified)

for i in range(len(y)):
    if y[i] != y_pred[i]:
        plt.scatter(X[i, 0], X[i, 1], c=y[i], s=50, cmap='Dark2')
plt.legend(("0","1","miss"))

在此处输入图像描述

You can set them manually like this:您可以像这样手动设置它们:

plt.legend(("0","1","miss"))
ax = plt.gca()
leg = ax.get_legend()
leg.legendHandles[0].set_color('purple')
leg.legendHandles[1].set_color('red')
leg.legendHandles[2].set_color('green')
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM