简体   繁体   中英

Multiple plotting from dataframe using seaborn

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!

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM