https://teratail.com/questions/234747 にてグラフ内に数値を入れる方法を hayataka2049さんが回答してくださっているのですから、その数値を描画する部分を Label名に変更するとよいのではないでしょうか。
あと、従来の凡例を非表示にするには plot.bar()
の引数に legend=False
パラメータをお渡しください。
Python
1import numpy as np
2import pandas as pd
3import matplotlib.pyplot as plt
4
5df = pd.DataFrame({'Sample No.':[1,2,3,4,5,6],
6 'Black':[30,45,10,15,40,30],
7 'Green': [20,30,60,40,45,45],
8 'Red': [50,25,30,45,15,25]})
9df = df.set_index('Sample No.')
10ax = plt.subplot()
11df.plot.bar(stacked=True, color=['black', 'green', 'red'], legend=False, ax=ax)
12ax.set_ylabel('Rate')
13
14# グラフ内に数値を入れるコードの一部を修正し、ラベルを入れるようにする
15for i, row in df.iterrows():
16 cs = np.cumsum(row)
17 cs_ = np.hstack([[0], cs[:-1]])
18 positions = (cs + cs_) / 2
19 for (label, v), p in zip(row.items(), positions):
20 ax.text(i - 1, p, label, color="white",
21 horizontalalignment="center",
22 verticalalignment="center")
23plt.show()
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2020/01/13 03:10
退会済みユーザー
2020/01/13 03:19