ongenz
ongenz

Reputation: 930

Add hue category labels in seaborn stripplot

I have two DataFrames that I am plotting as a stripplot. I am able to plot them pretty much as I wish, but I would like to know if it is possible to add the category labels for the "hue".

The plot currently looks like this:

Example plot.

However, I would like to add the labels of the categories (there are only two of them) to each "column" for each letter. So that it looks something like this:

enter image description here

The DataFrames look like this (although these are just edited snippets):

     Case Letter Size Weight
0   upper      A   20   bold
1   upper      A   23   bold
2   lower      A   61   bold
3   lower      A   62   bold
4   upper      A   78   bold
5   upper      A   95   bold
6   upper      B   23   bold
7   upper      B   40   bold
8   lower      B   47   bold
9   upper      B   59   bold
10  upper      B   61   bold
11  upper      B   99   bold
12  lower      C   23   bold
13  upper      D   23   bold
14  upper      D   66   bold
15  lower      D   99   bold
16  upper      E    5   bold
17  upper      E   20   bold
18  upper      E   21   bold
19  upper      E   22   bold

...and...

     Case Letter Size  Weight
0   upper      A    4  normal
1   upper      A    6  normal
2   upper      A    7  normal
3   upper      A    8  normal
4   upper      A    9  normal
5   upper      A   12  normal
6   upper      A   25  normal
7   upper      A   26  normal
8   upper      A   38  normal
9   upper      A   42  normal
10  lower      A   43  normal
11  lower      A   57  normal
12  lower      A   90  normal
13  upper      B    4  normal
14  lower      B    6  normal
15  upper      B    8  normal
16  upper      B    9  normal
17  upper      B   12  normal
18  upper      B   21  normal
19  lower      B   25  normal

The relevant code I have is:

fig, ax = plt.subplots(figsize=(10, 7.5))
plt.tight_layout()

sns.stripplot(x=new_df_normal['Letter'], y=new_df_normal['Size'], 
              hue=new_df_normal['Case'], jitter=False, dodge=True, 
              size=8, ax=ax, marker='D',
              palette={'upper': 'red', 'lower': 'red'})

plt.setp(ax.get_legend().get_texts(), fontsize='16') # for legend text
plt.setp(ax.get_legend().get_title(), fontsize='18') # for legend title

ax.set_xlabel("Letter", fontsize=20)
ax.set_ylabel("Size", fontsize=20)
ax.set_ylim(0, 105)
ax.tick_params(labelsize=20)

ax2 = ax.twinx()

sns.stripplot(x=new_df_bold['Letter'], y=new_df_bold['Size'], 
              hue=new_df_bold['Case'], jitter=False, dodge=True, 
              size=8, ax=ax2, marker='D',
              palette={'upper': 'green', 'lower': 'green'})

ax.legend_.remove()
ax2.legend_.remove()

ax2.set_xlabel("", fontsize=20)
ax2.set_ylabel("", fontsize=20)
ax2.set_ylim(0, 105)
ax2.tick_params(labelsize=20)

Is it possible to add those category labels ("bold" and "normal") for each column?

Upvotes: 6

Views: 3975

Answers (2)

pr94
pr94

Reputation: 1393

Set dodge=True enables this:

import seaborn as sns

tips = sns.load_dataset("tips")

sns.violinplot(x="day", y="total_bill", hue="smoker",
               data=tips, palette="muted")

sns.stripplot(x="day", y="total_bill", hue="smoker",
               data=tips, palette="muted", dodge=True)

enter image description here

EDIT: And with the df provided by the OP:

df = pd.read_csv('./ongenz.tsv', sep='\t')

sns.stripplot(x=df['Letter'], y=df['Size'], data=df, hue=df['Case'], dodge=True)

enter image description here

Upvotes: 2

LoneWanderer
LoneWanderer

Reputation: 3341

Using seaborn’s scatter plot you could access to the style (or even size) parameter. But you might not end up with your intended layout in the end. scatterplot documentation.

Or you could use the catplot and play with rows and columns. seaborn doc for catplot

Unfortunately Seaborn does not natively provide what you are looking for : another level of nesting beyond the hue parameter in stripplot (see stripplot documentation. Some seaborn tickets are opened that might be related, eg this ticket. But I’ve come accros some similar feature requests in seaborn that were refused, see this ticket

One last possibility is to dive into the matplotlib primitives to manipulate your seaborn diagram (since seaborn is just on top of matplotlib). Needless to say it would require a lot of effort, and might end-up nullifying seaborn in the first place ;)

Upvotes: 1

Related Questions