seaborn FacetGrid,在行和col上分开排列的barplot(seaborn FacetGrid , ranked barplot separated on row and col)

编程入门 行业动态 更新时间:2024-10-27 18:32:31
seaborn FacetGrid,在行和col上分开排列的barplot(seaborn FacetGrid , ranked barplot separated on row and col)

鉴于一些数据:

pt = pd.DataFrame({'alrmV':[000,000,000,101,101,111,111], 'he':[e,e,e,e,h,e,e], 'inc':[0,0,0,0,0,1,1]})

我想创建一个在行和列上分隔的条形图。

g = sns.FacetGrid(pt, row='inc', col='he', margin_titles=True) g.map( sns.barplot(pt['alrmV']), color='steelblue')

这有效,但我如何添加:

有序的x轴 仅显示前两个计数的alrmV类型

为了得到一个有序的x轴,显示前两种计数类型,我玩这个分组,但无法将其放入Facet网格:

grouped = pt.groupby( ['he','inc'] ) grw= grouped['alrmV'].value_counts().fillna(0.) #.unstack().fillna(0.) grw[:2].plot(kind='bar')

使用FacetGrid,切片限制显示的总计数

g.map(sns.barplot(pt['alrmV'][:10]), color='steelblue')

那么我怎么能得到一个条形图,它在行和列上分开,并且是有序的,只显示前2个计数?

Given some data:

pt = pd.DataFrame({'alrmV':[000,000,000,101,101,111,111], 'he':[e,e,e,e,h,e,e], 'inc':[0,0,0,0,0,1,1]})

I would like to create a bar plot separated on row and col.

g = sns.FacetGrid(pt, row='inc', col='he', margin_titles=True) g.map( sns.barplot(pt['alrmV']), color='steelblue')

This, works, but how do I also add:

an ordered x-axis only display the top-two-by-count alrmV types

To get an ordered x-axis, that displays the top 2 count types, I played around with this grouping, but unable to get it into a Facet grid:

grouped = pt.groupby( ['he','inc'] ) grw= grouped['alrmV'].value_counts().fillna(0.) #.unstack().fillna(0.) grw[:2].plot(kind='bar')

Using FacetGrid, slicing limits the total count displayed

g.map(sns.barplot(pt['alrmV'][:10]), color='steelblue')

So how can I get a bar graph, that is separated on row and col, and is ordered and displays only top 2 counts?

最满意答案

我无法使用您提供的数据示例,因此我将使用其中一个示例数据集来演示:

import seaborn as sns tips = sns.load_dataset("tips")

我们将在列中创建一个sex的情节,在行中smoker ,使用day作为条形图的x变量。 为了获得前两天的顺序,我们可以做到

top_two_ordered = tips.day.value_counts().order().index[-2:]

然后,您可以将此列表传递给x_order参数。

虽然你可以在这里直接使用FacetGrid ,但使用factorplot函数可能更容易:

g = sns.factorplot("day", col="sex", row="smoker", data=tips, margin_titles=True, size=3, x_order=top_two_ordered)

哪个绘制:

在此处输入图像描述

虽然我不建议您完全按照您的建议(在每个方面绘制不同x值的条形图),但可以通过执行类似的操作来完成

g = sns.FacetGrid(tips, col="sex", row="smoker", sharex=False) def ordered_barplot(data, **kws): x_order = data.day.value_counts().order().index[-2:] sns.barplot(data.day, x_order=x_order) g.map_dataframe(ordered_barplot)

制作

在此处输入图像描述

I couldn't get the example to work with the data you provided, so I'll use one of the example datasets to demonstrate:

import seaborn as sns tips = sns.load_dataset("tips")

We'll make a plot with sex in the columns, smoker in the rows, using day as the x variable for the barplot. To get the top two days in order, we could do

top_two_ordered = tips.day.value_counts().order().index[-2:]

Then you can pass this list to the x_order argument of barplot.

Although you can use FacetGrid directly here, it's probably easier to use the factorplot function:

g = sns.factorplot("day", col="sex", row="smoker", data=tips, margin_titles=True, size=3, x_order=top_two_ordered)

Which draws:

enter image description here

While I wouldn't recommend doing exactly what you proposed (plotting bars for different x values in each facet), it could be accomplished by doing something like

g = sns.FacetGrid(tips, col="sex", row="smoker", sharex=False) def ordered_barplot(data, **kws): x_order = data.day.value_counts().order().index[-2:] sns.barplot(data.day, x_order=x_order) g.map_dataframe(ordered_barplot)

to make

enter image description here

更多推荐

本文发布于:2023-07-27 14:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1291683.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在行   排列   col   seaborn   FacetGrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!