Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

limit the number of groups shown in seaborn countplot?

Tags:

Is it possible to show only the top/bottom n groups in asns.countplot()?

Using an example from the seaborn website,

sns.countplot(y="deck", hue="class", data=titanic, palette="Greens_d"); 

enter image description here

Is there any easy (or even relatively straightforward) way of limiting this plot to just 3 decks (groups) instead of displaying all 7 or is this something that would be better accomplished with an sns.bargraph or just plain matplotlib?

like image 248
scribbles Avatar asked Oct 01 '15 15:10

scribbles


People also ask

How do you show count in Countplot Seaborn?

countplot() method is used to Show the counts of observations in each categorical bin using bars. Parameters : This method is accepting the following parameters that are described below: x, y: This parameter take names of variables in data or vector data, optional, Inputs for plotting long-form data.

What does SNS Countplot () do?

When you use sns. countplot , Seaborn literally counts the number of observations per category for a categorical variable, and displays the results as a bar chart.

Can SNS Countplot () be used to see the frequency distribution of a categorical variable?

countplot. Show the counts of observations in each categorical bin using bars. A count plot can be thought of as a histogram across a categorical, instead of quantitative, variable.


2 Answers

import seaborn as sns titanic = sns.load_dataset("titanic") sns.countplot(y="deck", hue="class", data=titanic, palette="Greens_d",               order=titanic.deck.value_counts().iloc[:3].index) 

enter image description here

like image 150
mwaskom Avatar answered Sep 20 '22 15:09

mwaskom


Just adding real example instead of toy dataset. Assuming you have Pandas Data Frame name training_var and you want to display top 10 'Gene' column counts 'order=' bit should look as follows:

sb.countplot(x='Gene',data=training_var,order=pd.value_counts(training_var['Gene']).iloc[:10].index) 
like image 37
Error Replicator Avatar answered Sep 18 '22 15:09

Error Replicator