python:具有多个分布的distplot [英] python: distplot with multiple distributions

查看:46
本文介绍了python:具有多个分布的distplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 seaborn 绘制分布图.我想用不同的颜色在同一个图上绘制多个分布:

I am using seaborn to plot a distribution plot. I would like to plot multiple distributions on the same plot in different colors:

这是我如何开始分布图:

Here's how I start the distribution plot:

import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
iris = load_iris()
iris = pd.DataFrame(data= np.c_[iris['data'], iris['target']],columns= iris['feature_names'] + ['target'])

sns.distplot(iris[['sepal length (cm)']], hist=False, rug=True);

'target' 列包含 3 个值:0,1,2.

The 'target' column contains 3 values: 0,1,2.

我希望看到一个萼片长度分布图,其中目标 ==0、目标 ==1 和目标 ==2,总共 3 个图.

I would like to see one distribution plot for sepal length where target ==0, target ==1, and target ==2 for a total of 3 plots.

有人知道我是怎么做到的吗?

Does anyone know how I do that?

谢谢.

推荐答案

重要的是按值对数据框进行排序,其中 target0, 12.

The important thing is to sort the dataframe by values where target is 0, 1, or 2.

import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
import seaborn as sns

iris = load_iris()
iris = pd.DataFrame(data=np.c_[iris['data'], iris['target']],
                    columns=iris['feature_names'] + ['target'])

# Sort the dataframe by target
target_0 = iris.loc[iris['target'] == 0]
target_1 = iris.loc[iris['target'] == 1]
target_2 = iris.loc[iris['target'] == 2]

sns.distplot(target_0[['sepal length (cm)']], hist=False, rug=True)
sns.distplot(target_1[['sepal length (cm)']], hist=False, rug=True)
sns.distplot(target_2[['sepal length (cm)']], hist=False, rug=True)

plt.show()

输出如下:

如果您不知道 target 可能有多少个值,请在 target 列中找到唯一值,然后对数据框进行切片并适当地添加到图中.

If you don't know how many values target may have, find the unique values in the target column, then slice the dataframe and add to the plot appropriately.

import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
import seaborn as sns

iris = load_iris()
iris = pd.DataFrame(data=np.c_[iris['data'], iris['target']],
                    columns=iris['feature_names'] + ['target'])

unique_vals = iris['target'].unique()  # [0, 1, 2]

# Sort the dataframe by target
# Use a list comprehension to create list of sliced dataframes
targets = [iris.loc[iris['target'] == val] for val in unique_vals]

# Iterate through list and plot the sliced dataframe
for target in targets:
    sns.distplot(target[['sepal length (cm)']], hist=False, rug=True)

sns.plt.show()

这篇关于python:具有多个分布的distplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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