Seaborn堆积直方图/条形图

时间:2018-12-22 20:26:16

标签: python pandas matplotlib seaborn

我有一个pandas.DataFrame,我想根据两列绘制一个图形:Age(int),Survived(int-0或{{1} }。现在我有这样的东西:

enter image description here

这是我使用的代码:

1

因此,这在两个子图中显示。这很好,但是对于特定的年龄范围,在class DataAnalyzer: def _facet_grid(self, func, x: List[str], col: str = None, row: str = None) -> None: g = sns.FacetGrid(self.train_data, col=col, row=row) if func == sns.barplot: g.map(func, *x, ci=None) else: g.map(func, *x) g.add_legend() plt.show() def analyze(self) -> None: # Check if survival rate is connected with Age self._facet_grid(plt.hist, col='Survived', x=['Age']) 列中具有01的记录数量之间的区别就更难了。

所以我想要这样的东西:

enter image description here

在这种情况下,您会看到这种差异。有什么方法可以在Survived上进行操作(因为在那里我可以轻松地在seaborn上进行操作)?如果可能的话,我不想使用香草pandas.DataFrame

2 个答案:

答案 0 :(得分:2)

只需将总直方图与幸存的-0叠加即可。如果没有精确的数据框形式,很难给出确切的功能,但这是一个带有示例数据集的基本示例。

import matplotlib.pyplot as plt 
import seaborn as sns 
tips = sns.load_dataset("tips") 
sns.distplot(tips.total_bill, color="gold", kde=False, hist_kws={"alpha": 1}) 
sns.distplot(tips[tips.sex == "Female"].total_bill, color="blue", kde=False, hist_kws={"alpha":1}) 
plt.show()

答案 1 :(得分:2)

从seaborn 0.11.0开始,您可以这样做

# stacked histogram
import matplotlib.pyplot as plt
f = plt.figure(figsize=(7,5))
ax = f.add_subplot(1,1,1)

# mock your data frame
import pandas as pd
import numpy as np
_df = pd.DataFrame({
    "age":np.random.normal(30,30,1000),
    "survived":np.random.randint(0,2,1000)
})

# plot
import seaborn as sns
sns.histplot(data=_df, ax=ax, stat="count", multiple="stack",
             x="age", kde=False,
             palette="pastel", hue="survived",
             element="bars", legend=True)
ax.set_title("Seaborn Stacked Histogram")
ax.set_xlabel("Age")
ax.set_ylabel("Count")

enter image description here

相关问题