Categorical Scatter Plot

Often time, we have to represent relationships of x and y values that are partitioned by some category. In this section, I demonstrate the implementation of a categorical scatter plot using the iris dataset.

Importing the datasets and relevant packages.

import seaborn as sns
import pandas as pd
from sklearn import datasets
import matplotlib.pyplot as plt

%matplotlib inline
%config InlineBackend.figure_format = 'retina'

Loading some data to dataframe variable.

iris = datasets.load_iris()
iris_data = pd.DataFrame(iris.data, columns=iris.feature_names)

decode_species = {0: 'Setosa', 1:'Versicolor', 2:'Virginica' }
iris_data['Species'] = [decode_species.get(specie) for specie in iris.target]
iris_data.head()

Plotting the categorical plot

sns.set(rc={'figure.figsize':(10,8)})
sns.scatterplot(x='sepal length (cm)', y="sepal width (cm)", hue='Species', data=iris_data)
plt.legend(bbox_to_anchor=(1.2, 0.6))
_ = plt.title('Sepal Length vs Sepal Width by Species')