2

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.

beginner
  • 411
  • 4
  • 14

1 Answers1

1

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

Stef
  • 28,728
  • 2
  • 24
  • 52
  • Thank you. I am getting the following error, AttributeError: 'PathCollection' object has no attribute 'get_fc' while trying to replicate your code. What's wrong here? – beginner Jul 17 '20 at 19:49
  • Hmm, what matplotlib version do you have? I use `3.2.1` and it has [`get_fc()`](https://matplotlib.org/3.2.1/api/collections_api.html#matplotlib.collections.PatchCollection.get_facecolor) – Stef Jul 17 '20 at 19:55