
236
10 章 Matplotlib を使ったデータの可視化
Matplotlib
では、積み上げ棒グラフを簡単に作成できます
†
。例
10-8
は、図
10-6
を積み上げ形
式に変換します。図
10-8
にその結果を示します。
bar
で
bottom
引数を使い、上に乗せる棒の下端
を前のグループの上端に設定するのがポイントです。
例
10-8
例
10-6
の積み上げ棒グラフへの変換
# ...
bar_width = 0.8 #
棒幅
xlocs = np.arange(len(foo_data))
ax.bar(xlocs, foo_data, bar_width, color='#fde0bc', #
❶
label='Fooland')
ax.bar(xlocs, bar_data, bar_width, color='peru', #
❷
label='Barland', bottom=foo_data)
# ---
ラベル、グリッド、タイトル、そして保存
ax.set_yticks(range(18))
ax.set_xticks(ticks=np.array(range(len(foo_data))) + bar_width/2)
ax.set_xticklabels(labels)
ax.set_xlim(-(1-bar_width), xlocs[-1]+1) #
❸
# ...
❶
foo_data
と
bar_data
棒グループは、同じ
x
位置を共有する。
❷
bar_data
グループの下端は
foo_data
の上端になり、積み上げ棒グラフを提供する。 ...