【发布时间】:2022-01-06 04:32:21
【问题描述】:
我尝试将 histplot 函数中的“multiple = stack”选项与 FacetGrid 一起使用,但我无法获得正确的绘图而没有错误。任何帮助/想法表示赞赏。
这是一个带有 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。
【问题讨论】:
-
@JohanC 非常感谢!