如何在Seaborn的地块旁边获取传说? [英] How to get legend next to plot in Seaborn?

查看:36
本文介绍了如何在Seaborn的地块旁边获取传说?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Seaborn 绘制 relplot,但在主图下打印了图例(和空轴图).

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):

这是我使用的代码:

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?

这是我的数据集表单(在 2 个屏幕截图中,用于捕获所有列.sale_price_millions"是目标列)

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

推荐答案

由于您未能提供最小、完整和可验证的示例,没有人可以给你一个最终的工作答案,因为我们无法重现你的身材.不过,您可以尝试指定放置图例的位置如下,看看它是否如您所愿

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))

这篇关于如何在Seaborn的地块旁边获取传说?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆