I have a pandas dataframe as the following (although with more rows and columns):
Index | LOC1 | LOC2 | LOC 3 |
---|---|---|---|
A | 0.054 | 1.2 | 0.00 |
B | 0.38 | 3.89 | 0.027 |
C | 3.07 | 2.67 | 1.635 |
D | 7.36 | 6.2 | 0.23 |
I was wondering if it’s possible to highlight stripplot dots that belong to a specific sample. In my dataframe samples are index names (‘A’, ‘B’…). So, for example, I would like to use a different color for values in the ‘C’ row. As I pass my dataset in a wide-form https://seaborn.pydata.org/generated/seaborn.stripplot.html , I guess I can’t use hue, but I wasn’t able to figure out any other way.
JavaScript
x
18
18
1
import pandas as pd
2
import seaborn as sns
3
import matplotlib.pyplot as plt
4
5
df = pd.DataFrame(
6
{
7
"Index": list("ABCD"),
8
"LOC1": [0.054, 0.38, 3.07, 7.36],
9
"LOC2": [1.2, 3.89, 2.67, 6.2],
10
"LOC3": [0.0, 0.027, 1.635, 0.23]
11
}
12
)
13
14
fig = plt.figure()
15
ax=sns.boxplot(data=df, showfliers=False, medianprops=dict(color='red', linewidth=3))
16
ax=sns.stripplot(data=df,jitter=True, size=12, color=".3")
17
plt.show()
18
Advertisement
Answer
You could reshape your dataframe then use ‘hue’, assuming ‘Index’ is in the dataframe index, then you need to reset_index
before melt:
JavaScript
1
10
10
1
import seaborn as sns
2
import matplotlib.pyplot as plt
3
4
fig = plt.figure(figsize=(10,10))
5
title = 'TEST'
6
fig.suptitle(title,y=0.92,fontsize=36)
7
ax=sns.boxplot(data=df, showfliers=False, medianprops=dict(color='red', linewidth=3))
8
dfm = df.reset_index().melt('Index')
9
ax=sns.stripplot(data=dfm, x='variable', y='value', hue='Index', jitter=True, size=12, linewidth=1)
10
Output:
JavaScript
1
11
11
1
import seaborn as sns
2
import matplotlib.pyplot as plt
3
4
df = df.replace({'A':'Other', 'C':'Other','D':'Other'})
5
fig = plt.figure(figsize=(10,10))
6
title = 'TEST'
7
fig.suptitle(title,y=0.92,fontsize=36)
8
ax=sns.boxplot(data=df, showfliers=False, medianprops=dict(color='red', linewidth=3))
9
dfm = df.reset_index().melt('Index')
10
ax=sns.stripplot(data=dfm, x='variable', y='value', hue='Index', jitter=True, size=12, linewidth=1)
11
Output: