简体   繁体   English

使用 seaborn 从 dataframe 进行多次绘图

[英]Multiple plotting from dataframe using seaborn

I am trying to improve my visualizations on Python.我正在尝试改进 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.但是,我希望单独绘制每个“家庭”(一个用于鼬科动物的 plot,一个用于猫科动物,一个用于犬科动物),而不是在同一个 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.但是,我不确定如何 go 关于这个。

Thanks!谢谢!

Use catplot() to combine a barplot() and a FacetGrid.使用 catplot() 组合 barplot() 和 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:使用 catplot() 比直接使用 FacetGrid 更安全,因为它确保跨方面的变量顺序同步:

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

See more details here .在此处查看更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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