0

I am trying to improve my visualizations on Python. Assume I have this data:

data = {'animal':['cat', 'tiger', 'leopard', 'dog'], 'family':['mustelids ','felidae','felidae', 'canidae'], 'family_pct':[6.06,33.33,9.09,12.12]} 

df = pd.DataFrame(data) 

I want to create a barplot as follows:

fig, ax = plt.subplots()

sns.barplot(x = 'family', y = 'family_pct', hue='animal', data = df)

However, I would like each "family" to be plotted separately (one plot for mustelids, one for felidae, and one for canidae) and not on the same plot. I effectively would like to loop the graph over every value of the family column. However, I am not sure how to go about this.

Thanks!

1
  • Check out sns' FacetGrid. Commented Sep 26, 2019 at 15:36

1 Answer 1

1

Use catplot() to combine a barplot() and a FacetGrid. This allows grouping within additional categorical variables. Using catplot() is safer than using FacetGrid directly, as it ensures synchronization of variable order across facets:

sns.catplot(x = 'family', y = 'family_pct', hue='animal', col='family', data = df, kind='bar', height=4, aspect=.7)

See more details here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.