-1

Random DataFrame just to show my issue :

numbers = pd.Series(np.random.choice(10,50), name='numbers')
colors=pd.Series(np.random.choice(['A','B'],50), name='colors')
df = pd.concat([numbers, colors], axis=1)

I'm plotting a histogram using Seaborn, legend displays just fine by default :

sns.histplot(data=df, x='numbers', hue='colors')
plt.show()

legend displays fine

When trying to move my legend using loc, I get an empty square in the desired location instead :

sns.histplot(data=df, x='numbers', hue='colors')
plt.legend(loc="upper left")
plt.show()

empty square instead of legend

How can I move my legend?

1 Answers1

1

This may help:

numbers = pd.Series(np.random.choice(10,50), name='numbers')
colors=pd.Series(np.random.choice(['A','B'],50), name='colors')
df = pd.concat([numbers, colors], axis=1)

ax = sns.histplot(data=df, x='numbers', hue='colors') #<========== returning ax
sns.move_legend(ax, "upper left") <============== calling the move_legend method

Output:

enter image description here

Further reading: https://seaborn.pydata.org/generated/seaborn.move_legend.html

learner
  • 603
  • 1
  • 3
  • 15