0

In a Seaborn scatter plot, I can adjust the spacing in the legend entries like so:

tips = sns.load_dataset('tips')
g = sns.scatterplot(data=tips, x="total_bill", y="tip", hue="time")
plt.legend(labelspacing=20)

How can I do this with a CDF plot? Running g = sns.ecdfplot(data=tips, x="total_bill", hue="time") gives a plot with the legend. I have tried the following without any luck.

plt.legend(labelspacing=20)
  • Finishes plot but removes the legend
  • Throws error No handles with labels found to put in legend.

g.get_legend().legend(labelspacing=20)
  • Doesn't plot
  • Throws AttributeError: 'Legend' object has no attribute 'legend'
M--
  • 25,431
  • 8
  • 61
  • 93
a11
  • 3,122
  • 4
  • 27
  • 66

1 Answers1

1

The latest seaborn 0.11.2 has a new function move_legend() which apart from moving the legend also allows changing other legend properties (note that axes-level functions such as sns.scatterplot and sns.ecdfplot return an ax):

import seaborn as sns

tips = sns.load_dataset('tips')
ax = sns.ecdfplot(data=tips, x="total_bill", hue="time")
sns.move_legend(ax, labelspacing=5, loc='best')

sns.ecdfplot with changed legend

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • Link to figure level vs. axes level plots was helpful, thank you. For those of us on 0.11.1, is there a way? I looked through [the code for `move_legend`](https://github.com/mwaskom/seaborn/blob/master/seaborn/utils.py) but still couldn't figure it out. `plt.setp(g.get_legend(), fontsize='22')` works, but `labelspacing=20` does not work. – a11 Oct 01 '21 at 16:38
  • 1
    For those on seaborn 0.11.1 there is `pip install seaborn --upgrade`. Note that `labelspacing` will change the size the box making it a non-trivial update. – JohanC Oct 01 '21 at 16:58
  • lol point taken – a11 Oct 01 '21 at 17:26