0

There is my code:

import matplotlib.pyplot as plt 
import numpy as np 
import seaborn as sns 
import pandas as pd 
from sklearn import datasets
data = datasets.load_iris(return_X_y=False)
X = data.data
y = data.target
    
names = data.feature_names 
target_names = data.target_names
columns=names+['target']
df = pd.DataFrame(np.hstack([X, y.reshape(-1,1)]), columns=columns) 
df.loc[df.target==0, 'target_names'] = 'setosa' 
df.loc[df.target==1, 'target_names'] = 'versicolor' 
df.loc[df.target==2, 'target_names'] = 'virginica'  
indexes = df.index.tolist()

fig,axes = plt.subplots(2,2,figsize=(12,8))
axes[0,0].scatter(indexes,df['sepal length (cm)'],c=y)
axes[0,1].scatter(indexes,df['sepal width (cm)'],c=y)
axes[1,0].scatter(indexes,df['petal length (cm)'],c=y)
axes[1,1].scatter(indexes,df['petal width (cm)'],c=y)
plt.show()

How to add legend to each scatter, where each item is value of y ?

harp1814
  • 1,494
  • 3
  • 13
  • 31

1 Answers1

0

As far as I understand there is no direct way of making the scatter with tags on each data point.

This answer suggests iterating over your data points and labels, once you have created the scatter plots:

for i, txt in enumerate(y):
    axes[0,0].annotate(txt, (indexes[i], df['sepal length (cm)'][i]))
    ...

You can look at formatting options here.

Ariane
  • 326
  • 2
  • 11