1

I have a seaborn python scatterplot with a legend that is too long to fit with the main plot. Is there a way to save the legend separately. I know it can be done if I used matplotlib.axes.scatter as in this gist but I would like to use seaborn as the representation is nicer.

%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

np.random.seed(7)

plt.figure(figsize=(16,10))
fig = sns.scatterplot(
    x="tsne-2d-one", 
    y="tsne-2d-two",
    hue="labels",
    palette=sns.color_palette("hls", 3995),
    data=df_subset,
    legend='brief',
    alpha=0.3,
)

any help would be appreciated

example image

Alex
  • 6,610
  • 3
  • 20
  • 38
SysEng
  • 43
  • 8
  • Have you tried implementing the code in the gist you linked? `sns.scatterplot` returns a `matplotlib.axes.Axes` so you should be able to access the legend in the same way. – Alex May 15 '21 at 13:52
  • Thanks Alex, I shall give that a try – SysEng May 15 '21 at 14:22
  • You could try `ax = sns.scatterplot(...)` and then access the field `legend = ax.legend_`. – JohanC May 15 '21 at 14:26

0 Answers0