chase
chase

Reputation: 3782

color/marker by index seaborn scatterplot

Is it possible to construct a scatter plot which has specific markers and coloring for the index (given as index here, or could be columns for transpose) with columns giving different classes. i.e. - within group coloring which maintains consistent between groups.

import pandas as pd
df = pd.DataFrame({'Class_1': [0.98, 0.93, 0.95], 'Class_2': [0.48, 0.43, 0.45],  'Class_3': [0.78, 0.73, 0.75]}, index = ['precision', 'recall', 'fscore'])
display(df)
import seaborn as sns
import matplotlib.pyplot as plt
sns.stripplot(data = data)
plt.show()

This produces the following:

enter image description here

However, I would like to understand the method to colorize by the index, as is done here:

enter image description here

Upvotes: 2

Views: 5152

Answers (1)

Serenity
Serenity

Reputation: 36635

Reorganize your dataframe from pivot table to classical table dataframe and use hue argument of stripplot like here:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.DataFrame({'Class_1': [0.98, 0.93, 0.95], 'Class_2': [0.48, 0.43, 0.45],  'Class_3': [0.78, 0.73, 0.75]}, index = ['precision', 'recall', 'fscore'])

# convert dataframe  
df2=df.stack()
df2 = df2.reset_index()
df2.columns = ['Index','Classes','Values']
print(df2)

sns.stripplot(data=df2,x='Classes',y='Values',hue='Index', palette="Set2")

plt.show()

df2:

       Index  Classes  Values
0  precision  Class_1    0.98
1  precision  Class_2    0.48
2  precision  Class_3    0.78
3     recall  Class_1    0.93
4     recall  Class_2    0.43
5     recall  Class_3    0.73
6     fscore  Class_1    0.95
7     fscore  Class_2    0.45
8     fscore  Class_3    0.75

enter image description here

Upvotes: 2

Related Questions