为什么在seaborn pairplot的kde子图中没有显示颜色? [英] Why are no colors shown in kde subplots in seaborn pairplot?

查看:93
本文介绍了为什么在seaborn pairplot的kde子图中没有显示颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看虹膜数据集(Fisher 1936).例如

I am looking at the iris data set (Fisher 1936). e.g. https://www.kaggle.com/uciml/iris/downloads/Iris.csv

Creating the seaborn pairplot with the arguments

sns.pairplot(iris.drop("Id", axis=1), diag_kind="kde", hue="Species")

return a pairplot with kde charts on the diagonals; However, I am missing the different colors for the different species in the kde plots, the scatters are fine & colorful.

My result is inline with the seaborn docs. http://seaborn.pydata.org/tutorial/axis_grids.html

g = sns.pairplot(iris, hue="species", palette="Set2", diag_kind="kde", size=2.5)

But there a several different examples published showong the colors. e.g. http://www.arunprakash.org/2017/06/data-visualisation-seaborn.html

sns.pairplot(iris, hue='Species', diag_kind='kde', size=2);

or https://www.kaggle.com/benhamner/python-data-visualizations

sns.pairplot(iris.drop("Id", axis=1), hue="Species", size=3, diag_kind="kde")

Has there been a recent change in the seaborn API (ver 0.8.0) ? Have the colors been removed on purpose? Is there a kw to the show them again?

解决方案

There was an the issue with producing hues on the diagonal of sns.pairplot. This issue is now fixed in version 0.8.1 of seaborn.

In case one is still interested, the following may be a workaround. You may create the underlying PairGrid yourself and map the diagonal and the off_diagonal elements individually. For the diagonal elements, get a color from the current cycler first, then use this color for the kdeplot.

import matplotlib.pyplot as plt
import seaborn as sns
iris = sns.load_dataset("iris")

g =  sns.PairGrid(iris, hue='species', size=2)

def f(x, **kwargs):
    kwargs.pop("color")
    col = next(plt.gca()._get_lines.prop_cycler)['color']
    sns.kdeplot(x, color=col, **kwargs)

g.map_diag(f)
g.map_offdiag(plt.scatter)
g.add_legend()
plt.show()

这篇关于为什么在seaborn pairplot的kde子图中没有显示颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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