【问题标题】:Seaborn: markers are not showing up [duplicate]Seaborn:标记未显示[重复]
【发布时间】:2022-01-03 21:33:17
【问题描述】:

使用找到的代码here,我正在尝试更改sns.scatterplot 标记,但无法使其工作。代码如下:

import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
import numpy as np
from sklearn.svm import SVC
import random
n = 15
X, y = make_classification(n_samples=20, n_features=2,
                           n_informative=2, n_redundant=0,
                           n_classes=2,
                           random_state=n)
X_train, X_test, y_train, y_test = train_test_split(X, y,
                                   test_size=0.3, stratify=y,
                                   random_state=32)

svc_model = SVC(kernel='linear', random_state=32)
svc_model.fit(X_train, y_train)

plt.figure(figsize=(4, 4))

sns.scatterplot(x=X_train[:, 0], 
                y=X_train[:, 1], 
                hue=y_train,
                markers = ['.','+']);

w = svc_model.coef_[0]
b = svc_model.intercept_[0]
x_points = np.linspace(-2, 2)
y_points = -(w[0] / w[1]) * x_points - b / w[1]
plt.plot(x_points, y_points, c='r')
plt.ylabel(r'$x_2$', usetex = True, fontsize = 15, rotation = 0)
plt.xlabel(r'$x_1$', usetex = True, fontsize = 15)

我运行该脚本,但标记没有改变。

【问题讨论】:

    标签: python seaborn scatter-plot markers


    【解决方案1】:

    标记将通过添加 style 参数显示:

    sns.scatterplot(x=X_train[:, 0],
                    y=X_train[:, 1],
                    hue=y_train,
                    style=y_train,
                    markers=['o', 'P'])
    

    请注意,并非所有标记组合都是允许的。参考:Filled and line art markers cannot be mixed

    输出:

    【讨论】:

      猜你喜欢
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      • 2014-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-26
      相关资源
      最近更新 更多