python - Seaborn pairplot 图例不显示颜色和标签
问题描述
我正在使用 seaborn 0.11.2,但我很难看到 seaborn 配对图的传说。这是代码:除图例外,一切正常
for x in x1_categorical:
plt.figure()
sns.pairplot(data=x1[[x,'weight']],hue=x, palette='husl', height=4, aspect=4)
plt.title(x)
我看不到颜色或标签。我已经尝试过这里的建议:Seaborn Pairplot Legend Not Showing Colors
我不知道,提前谢谢!
解决方案
如果我理解正确,则x1_categorical
包含分类列名。以 seaborn 的企鹅数据集为例,当前代码如下:
from matplotlib import pyplot as plt
import seaborn as sns
x1 = sns.load_dataset('penguins')
x1_categorical = ['species', 'island', 'sex']
for x in x1_categorical:
g = sns.pairplot(data=x1[[x, 'body_mass_g']], hue=x, palette='husl', height=4, aspect=3)
plt.title(x)
plt.tight_layout()
当我尝试这个(seaborn 0.11.2)时,我得到如下图:
这些似乎是数字列的 kdeplots,使用分类列作为色调。不幸的是,传说是空的,也当plt.legend()
被尝试过。
另一种方法是显式创建 kdeplots,例如:
from matplotlib import pyplot as plt
import seaborn as sns
x1 = sns.load_dataset('penguins')
x1_categorical = ['species', 'island', 'sex']
fig, axs = plt.subplots(ncols=1, nrows=len(x1_categorical), figsize=(12, 4*len(x1_categorical)))
for ax, x in zip(axs, x1_categorical):
sns.kdeplot(data=x1, x='body_mass_g', hue=x, palette='husl', fill=True, common_norm=False, ax=ax)
sns.despine()
示例代码创建了一个大图,但如果需要,也可以创建单独的图。
另一种方法可以使用common_norm=True, multiple='stack'
:
推荐阅读
- jupyter-notebook - Jupyter Notebook 不同的显示方式
- vue.js - 无法导入 vue-cli-service 构建的 Vue 组件
- html - 如何修复 ReferenceError:X 未定义
- react-native - 使用本机反应设置包含滚动视图的视图高度
- php - 如何将 WordPress 页面拆分为多个 URL?
- ios - 如何使用swift在collectionview中实现显示/隐藏部分
- sas - 如何解决此错误:“物理文件不存在”
- google-maps - 谷歌地图仅在网站上显示为第二
- python-3.x - 放置在 /usr/bin 中的 Python 脚本未将数据保存在 sqlite db 中
- python - 如何修复python中的“名称未定义”错误