Search code examples
pythonseabornscatter-plotmarkers

Seaborn: markers are not showing up


Using the code found here, i am trying to change the sns.scatterplot markers, but can't make it work. Here's the code:

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)

I run that script, but the markers doesn't change.


Solution

  • The markers will showing up by adding style argument :

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

    Note that not all combinations of markers are permitted. Reference: Filled and line art markers cannot be mixed

    Output:

    Out