gpt4 book ai didi

python - Seaborn FacetGrid 条形图和色调

转载 作者:太空狗 更新时间:2023-10-29 20:51:30 26 4
gpt4 key购买 nike

我有一个具有以下结构的 DataFrame:

interval    segment  variable        value
4 02:00:00 Night weekdays 154.866667
5 02:30:00 Night weekdays 100.666667
6 03:00:00 Night weekdays 75.400000
7 03:30:00 Night weekdays 56.533333
8 04:00:00 Night weekdays 55.000000
9 04:30:00 Night weekends 53.733333
10 05:00:00 Night weekends 81.200000
11 05:30:00 Night weekends 125.933333
14 07:00:00 Morning weekdays 447.200000
15 07:30:00 Morning weekends 545.200000
16 08:00:00 Morning weekends 668.733333
17 08:30:00 Morning weekends 751.333333
18 09:00:00 Morning weekdays 793.800000
19 09:30:00 Morning weekdays 781.125000
23 11:30:00 Noon weekdays 776.375000
24 12:00:00 Noon weekdays 741.812500
25 12:30:00 Noon weekends 723.000000
26 13:00:00 Noon weekends 734.562500
27 13:30:00 Noon weekends 763.882353
28 14:00:00 Afternoon weekdays 810.411765
31 15:30:00 Afternoon weekdays 855.411765
32 16:00:00 Afternoon weekdays 824.882353
33 16:30:00 Afternoon weekends 768.529412
34 17:00:00 Afternoon weekends 790.812500
35 17:30:00 Afternoon weekends 809.125000

我想生成条形图的多面网格,每个变量(工作日/周末)一个,然后根据“段”列为条形着色。

制作这两个条形图非常简单:

g = sns.FacetGrid(melted, col="variable")
g.map(sns.barplot,'interval','value')

这会产生(我知道 xlabels 是错误的,我可以更正): enter image description here

我坚持根据“段”为条形图着色。根据文档,我需要做的就是在实例化 FacetGrid 时添加变量并设置一些调色板:

g = sns.FacetGrid(melted, col="variable",hue="segment",palette="Set3")
g.map(sns.barplot,'interval','value')

但这会产生: enter image description here

条形图相互堆叠,而不是散布在整个区间内。我在这里缺少什么?

我创建了一个 gist与数据集。

最佳答案

因为 interval 嵌套在 x 变量(segment)中,你需要告诉 barplot 所有x 变量的可能级别,这样它们就不会被绘制在彼此之上:

times = df.interval.unique()
g = sns.FacetGrid(df, row="variable", hue="segment",
palette="Set3", size=4, aspect=2)
g.map(sns.barplot, 'interval', 'value', order=times)

enter image description here

关于python - Seaborn FacetGrid 条形图和色调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35230524/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com