scikit-learn示例中图例中的标记丢失

huangapple go评论64阅读模式
英文:

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()

现在的图例中只有文本,没有标记。

scikit-learn示例中图例中的标记丢失

英文:

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.

Example code :

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.

scikit-learn示例中图例中的标记丢失

答案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="右上",
)

scikit-learn示例中图例中的标记丢失

英文:
  • In a standard plot, label='weighted' might be specified; ax.scatter(..., label='weighted'), and the label is displayed with ax.legend().
  • However, DecisionBoundaryDisplay is a plot_method="contour", which doesn't accept the label 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",
)

scikit-learn示例中图例中的标记丢失

huangapple
  • 本文由 发表于 2023年5月30日 02:08:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定