Seaborn ecdf plot, adjust spacing in legend items

Question:

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'
Asked By: a11

||

Answers:

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

Answered By: JohanC