pandas の機能で以下の DataFrame を作成して から、catplot で kind="bar" として棒グラフを作成してください。
pandas による操作は以下
- groupby("sex") で性別でグループ化する。
- 列 "class" に対して value_counts(normalize=True) で割合を計算する。
- rename("percentage") と列名をリネームする。
- reset_index() で MultiIndex の Series を DataFrame にする。
python
1import seaborn as sns
2
3titanic = sns.load_dataset("titanic")
4
5df = (
6 titanic.groupby("sex")["class"]
7 .value_counts(normalize=True)
8 .rename("percentage")
9 .reset_index()
10)
11print(df)
12# sex class percentage
13# 0 female Third 0.458599
14# 1 female First 0.299363
15# 2 female Second 0.242038
16# 3 male Third 0.601386
17# 4 male First 0.211438
18# 5 male Second 0.187175
19
20sns.catplot(
21 x="class",
22 y="percentage",
23 col_order=["male", "female"],
24 order=["First", "Second", "Third"],
25 col="sex",
26 data=df,
27 kind="bar",
28)

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/19 02:15