Issue
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()
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()
How can I move my legend?
Solution
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:
Further reading: https://seaborn.pydata.org/generated/seaborn.move_legend.html
Answered By - learner
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.