Polishing Scatter Plot Legends With Seaborn

Viewed 171

The scatter plot generated for the piece of code below using seaborn is as follows.

ax = sns.scatterplot(x="Param_1", 
                     y="Param_2", 
                     hue="Process", style='Item', data=df,
                     s=30, legend='full')

Scatter plot

I wanted to get rid of color legends (for process) in circle as circles also denote data for Item 'One'. What would be the best way to present the colors legends for Process without making a discrepancy with shapes used for Item.

1 Answers

You can create so-called proxy artists and use them as legend symbols.

import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

fig,(ax1,ax2) = plt.subplots(ncols=2)
tips = sns.load_dataset("tips")

hue = "day"
style = "time"

sns.scatterplot(x="total_bill", y="tip", hue=hue, style=style, data=tips, ax=ax1)
ax1.set_title("Default Legend")
sns.scatterplot(x="total_bill", y="tip", hue=hue, style=style, data=tips, ax=ax2)
ax2.set_title("Custom Legend")

handles, labels = ax2.get_legend_handles_labels()
for i,label in enumerate(labels):
    if label == hue:
        continue
    if label == style:
        break
    handles[i] = mpatches.Patch(color=handles[i].get_fc()[0])
ax2.legend(handles, labels)

enter image description here

Related