"stack" 不适用于 seaborn 刻面图
"stack" does not work for seaborn facet plot
我尝试将 histplot 函数中的“multiple = stack”选项与 FacetGrid 一起使用,但我无法获取正确的情节没有错误。任何 help/thought 表示赞赏。
这是一个可重现的例子 w/ Python 3.8 + seaborn==0.11.2 + pandas==1.3.4
import pandas as pd
import seaborn as sns
# test data
data = [1, 1, 1, 2, 1, 1, 1, 2]
facet = [1, 1, 1, 1, 2, 2, 2, 2]
group = [1, 2] * 4
df = pd.DataFrame(data = {"data": data, "facet": facet, "group": group})
# single histplot can stacked
sns.histplot(data = df, x = "data", hue = "group", multiple = "stack")
# facet plot can not
g = sns.FacetGrid(df, col=None, row="facet", hue = "group")
g.map(sns.histplot, "data", **{"stat": "count", "multiple": "stack", "binwidth": 0.1})
# also tried this
# g.map_dataframe(sns.histplot, "data", multiple = "stack", binwidth = .1)
g.add_legend()
这是有效的单个 histplot 的输出:
但我不能与 facetgrid 一起做:请注意,如果堆叠正确,两个面应该是 3 而不是 2。
终于!诀窍是将“色调”放在 map_dataframe(这里不能是地图)语句而不是 FacetGrid 语句中。
g = sns.FacetGrid(df, row="facet")
g.map_dataframe(sns.histplot, x = "data", hue = "group", multiple = "stack", binwidth = .1)
我尝试将 histplot 函数中的“multiple = stack”选项与 FacetGrid 一起使用,但我无法获取正确的情节没有错误。任何 help/thought 表示赞赏。
这是一个可重现的例子 w/ Python 3.8 + seaborn==0.11.2 + pandas==1.3.4
import pandas as pd
import seaborn as sns
# test data
data = [1, 1, 1, 2, 1, 1, 1, 2]
facet = [1, 1, 1, 1, 2, 2, 2, 2]
group = [1, 2] * 4
df = pd.DataFrame(data = {"data": data, "facet": facet, "group": group})
# single histplot can stacked
sns.histplot(data = df, x = "data", hue = "group", multiple = "stack")
# facet plot can not
g = sns.FacetGrid(df, col=None, row="facet", hue = "group")
g.map(sns.histplot, "data", **{"stat": "count", "multiple": "stack", "binwidth": 0.1})
# also tried this
# g.map_dataframe(sns.histplot, "data", multiple = "stack", binwidth = .1)
g.add_legend()
这是有效的单个 histplot 的输出:
但我不能与 facetgrid 一起做:请注意,如果堆叠正确,两个面应该是 3 而不是 2。
终于!诀窍是将“色调”放在 map_dataframe(这里不能是地图)语句而不是 FacetGrid 语句中。
g = sns.FacetGrid(df, row="facet")
g.map_dataframe(sns.histplot, x = "data", hue = "group", multiple = "stack", binwidth = .1)