python - 如何将图例移到 seaborn 散点图之外?
问题描述
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
sns.set(style="darkgrid")
g = sns.scatterplot(x="Area", y="Rent/Sqft", hue="region", style="availability", data=df)
当我运行它时,我得到下面的情节。
我想将图例移到情节之外。我用谷歌搜索并尝试了以下
g.legend(loc='right', bbox_to_anchor=(1.25, 0.5), ncol=1)
plt.show()
但我没有得到任何输出。此外,我无法理解对象 plt 是如何连接到我的 sns 对象的
我正在使用 Jupyter Notebook、Python 3.6 和 Seaborn 0.9.0。
解决方案
请尝试以下方法:
g.legend(loc='center left', bbox_to_anchor=(1.25, 0.5), ncol=1)
如果需要,您可以将第一个数字更改为负数,以便将图例放在左侧。
如果您使用的是 Jupyter IDE,则需要将两行代码放在同一个单元格中并一起运行以获得输出。此外,没有sns
对象这样的东西。seaborn中的大多数函数都返回一个 matplotlibAxes
对象,您可以在其中使用与该对象关联的所有方法Axes
,例如您在此处使用的方法(即.legend()
)。
推荐阅读
- batch-file - 在批处理文件中添加一个 reg 有问题
- python - Python如何在执行后使用autopygui将函数加倍
- python - 从元组列表中为每个项目创建 np.arrays 的最快方法
- c++ - 我是否必须将经过测试的源文件添加到 VS2017 中的单元测试项目中?
- wcf - 主机启动并运行,但无法从客户端创建服务引用
- excel - 在另一个工作表中搜索第一个工作表名称并插入该列数据
- python - 如何制作计数向量 - Python
- mysql - 更新查询计数的内部联接
- laravel - 当我们在 Laravel 5.7 中使用条件子句时如何用括号包装 Eloquent 查询
- angular - 角。如何将现有的控件值访问器包装在我自己的控件中?