How to get legend next to plot in Seaborn?

你离开我真会死。 提交于 2019-12-13 03:54:23

问题


I am plotting a relplot with Seaborn, but getting the legend (and an empty axis plot) printed under the main plot.

Here is how it looks like (in 2 photos, as my screen isn't that big):

Here is the code I used:

fig, axes = plt.subplots(1, 1, figsize=(12, 5))

clean_df['tax_class_at_sale'] = clean_df['tax_class_at_sale'].apply(str)

sns.relplot(x="sale_price_millions", y='gross_sqft_thousands', hue="neighborhood", data=clean_df, ax=axes)

fig.suptitle('Sale Price by Neighborhood', position=(.5,1.05), fontsize=20)
fig.tight_layout()
fig.show()

Does someone has an idea how to fix that, so that the legend (maybe much smaller, but it's not a problem) is printed next to the plot, and the empty axis disappears?

Here is my dataset form (in 2 screenshot, to capture all columns. "sale_price_millions" is the target column)


回答1:


Since you failed to provide a Minimal, Complete, and Verifiable example, no one can give you a final working answer because we can't reproduce your figure. Nevertheless, you can try specifying the location for placing the legend as following and see if it works as you want

sns.relplot(x="sale_price_millions", y='gross_sqft_thousands', hue="neighborhood", data=clean_df, ax=axes)
plt.legend(loc=(1.05, 0.5))


来源:https://stackoverflow.com/questions/56417430/how-to-get-legend-next-to-plot-in-seaborn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!