鉴于一些数据:
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 typesTo 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:
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
更多推荐
发布评论