Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

seaborn distplot / displot with multiple distributions

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'])     sepal length (cm)  sepal width (cm)  petal length (cm)  petal width (cm)  target 0                5.1               3.5                1.4               0.2     0.0 1                4.9               3.0                1.4               0.2     0.0 2                4.7               3.2                1.3               0.2     0.0 3                4.6               3.1                1.5               0.2     0.0 4                5.0               3.6                1.4               0.2     0.0  sns.distplot(iris[['sepal length (cm)']], hist=False, rug=True); 

enter image description here

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

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.

like image 733
Trexion Kameha Avatar asked Sep 05 '17 01:09

Trexion Kameha


People also ask

What is the difference between Displot and Distplot?

displot() is the new distplot() with better capabilities and distplot() is deprecated starting from this Seaborn version. With the new displot() function in Seaborn, the plotting function hierarchy kind of of looks like this now covering most of the plotting capabilities.

Is Distplot deprecated?

Staring with version 0.11. 0 of the Python Seaborn data visualization library, distplot is a deprecated function, and will be removed in a future version.

What can I use instead of a Distplot?

For simple plots where x is a vector of data, displot(x) and histplot(x) are drop-in replacements for distplot(x) . Both functions also allow you to add a KDE curve by setting kde=True . As an enhancement, the new functions will plot a KDE over a histogram that shows counts, rather than requiring density normalization.


1 Answers

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

The output looks like:

enter image description here

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) 
like image 145
Arda Arslan Avatar answered Sep 20 '22 06:09

Arda Arslan