英文:
Missing markers in the plot legends of scikit-learn examples
问题
我一直在查看Scikit库的文档和示例代码。许多图表在图例中没有标记,让我们猜测一切。
示例代码:
import matplotlib.pyplot as plt
from sklearn import svm
from sklearn.datasets import make_blobs
from sklearn.inspection import DecisionBoundaryDisplay
# 创建两个随机点的聚类
n_samples_1 = 1000
n_samples_2 = 100
centers = [[0.0, 0.0], [2.0, 2.0]]
clusters_std = [1.5, 0.5]
X, y = make_blobs(
n_samples=[n_samples_1, n_samples_2],
centers=centers,
cluster_std=clusters_std,
random_state=0,
shuffle=False,
)
# 拟合模型并获取分离超平面
clf = svm.SVC(kernel="linear", C=1.0)
clf.fit(X, y)
# 拟合模型并获取带有加权类别的分离超平面
wclf = svm.SVC(kernel="linear", class_weight={1: 10})
wclf.fit(X, y)
# 绘制样本点
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired, edgecolors="k")
# 绘制两个分类器的决策函数
ax = plt.gca()
disp = DecisionBoundaryDisplay.from_estimator(
clf,
X,
plot_method="contour",
colors="k",
levels=[0],
alpha=0.5,
linestyles=["-"],
ax=ax,
)
# 绘制带有加权类别的决策边界和边距
wdisp = DecisionBoundaryDisplay.from_estimator(
wclf,
X,
plot_method="contour",
colors="r",
levels=[0],
alpha=0.5,
linestyles=["-"],
ax=ax,
)
plt.legend(
[disp.surface_.collections[0], wdisp.surface_.collections[0]],
["non weighted", "weighted"],
loc="upper right",
)
plt.show()
现在的图例中只有文本,没有标记。
英文:
I have been looking at the Scikit library documentation and example codes. Many of the plots does not have markers in the legends, leaving us to guess everything.
import matplotlib.pyplot as plt
from sklearn import svm
from sklearn.datasets import make_blobs
from sklearn.inspection import DecisionBoundaryDisplay
# we create two clusters of random points
n_samples_1 = 1000
n_samples_2 = 100
centers = [[0.0, 0.0], [2.0, 2.0]]
clusters_std = [1.5, 0.5]
X, y = make_blobs(
n_samples=[n_samples_1, n_samples_2],
centers=centers,
cluster_std=clusters_std,
random_state=0,
shuffle=False,
)
# fit the model and get the separating hyperplane
clf = svm.SVC(kernel="linear", C=1.0)
clf.fit(X, y)
# fit the model and get the separating hyperplane using weighted classes
wclf = svm.SVC(kernel="linear", class_weight={1: 10})
wclf.fit(X, y)
# plot the samples
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired, edgecolors="k")
# plot the decision functions for both classifiers
ax = plt.gca()
disp = DecisionBoundaryDisplay.from_estimator(
clf,
X,
plot_method="contour",
colors="k",
levels=[0],
alpha=0.5,
linestyles=["-"],
ax=ax,
)
# plot decision boundary and margins for weighted classes
wdisp = DecisionBoundaryDisplay.from_estimator(
wclf,
X,
plot_method="contour",
colors="r",
levels=[0],
alpha=0.5,
linestyles=["-"],
ax=ax,
)
plt.legend(
[disp.surface_.collections[0], wdisp.surface_.collections[0]],
["non weighted", "weighted"],
loc="upper right",
)
plt.show()
Present plot: In the below plot legend, only text is present, no markers.
答案1
得分: 1
- 在标准绘图中,可能会指定
label='weighted'
;ax.scatter(..., label='weighted')
,然后使用ax.legend()
显示标签。 - 但是,
DecisionBoundaryDisplay
是一个plot_method="contour"
,不接受label
参数。contour
不使用以下参数:'label'
- 因此,根据如何手动创建图例创建自定义标签处理程序。
from matplotlib.lines import Line2D
# 根据重复部分,创建适当的线条处理程序
# disp.surface_.collections[0] 和 wdisp.surface_.collections[0] 不是有颜色的标签
plt.legend(
[Line2D([0], [0], color='k'), Line2D([0], [0], color='r')],
["非加权", "加权"],
loc="右上",
)
# 或者
plt.legend(
[Line2D([0], [0], color=c) for c in ['k', 'r']],
["非加权", "加权"],
loc="右上",
)
英文:
- In a standard plot,
label='weighted'
might be specified;ax.scatter(..., label='weighted')
, and the label is displayed withax.legend()
. - However,
DecisionBoundaryDisplay
is aplot_method="contour"
, which doesn't accept thelabel
parameter.The following kwargs were not used by contour: 'label'
- As such, create a custom label handle, as per How to manually create a legend
from matplotlib.lines import Line2D
# as per the duplicate, create a proper line handle
# disp.surface_.collections[0] and wdisp.surface_.collections[0] are not colored handles
plt.legend(
[Line2D([0], [0], color='k'), Line2D([0], [0], color='r')],
["non weighted", "weighted"],
loc="upper right",
)
# or
plt.legend(
[Line2D([0], [0], color=c) for c in ['k', 'r']],
["non weighted", "weighted"],
loc="upper right",
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论