Question

[Solved] limit the number of groups shown in seaborn countplot?

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?

Solution #1:

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

Respondent: mwaskom

Solution #2:

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)
Respondent: Error Replicator

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy