使用Pandas條形圖上的值注釋條形圖我正在尋找一種方法來使用我的DataFrame中的舍入數(shù)值來在Pandas條形圖中注釋我的條形圖。>>> df=pd.DataFrame({'A':np.random.rand(2),'B':np.random.rand(2)},index=['value1','value2'] ) >>> df
A B
value1 0.440922 0.911800
value2 0.588242 0.797366我想得到這樣的東西:我嘗試使用此代碼示例,但注釋都集中在x刻度上:>>> ax = df.plot(kind='bar') >>> for idx, label in enumerate(list(df.index)):
for acc in df.columns:
value = np.round(df.ix[idx][acc],decimals=2)
ax.annotate(value,
(idx, value),
xytext=(0, 15),
textcoords='offset points')
2 回答

飲歌長嘯
TA貢獻(xiàn)1951條經(jīng)驗 獲得超3個贊
采用樣品浮法成型處理的解決方案也可以處理負(fù)值。
仍然需要調(diào)整補(bǔ)償。
df=pd.DataFrame({'A':np.random.rand(2)-1,'B':np.random.rand(2)},index=['val1','val2'] )ax = df.plot(kind='bar', color=['r','b']) x_offset = -0.03y_offset = 0.02for p in ax.patches: b = p.get_bbox() val = "{:+.2f}".format(b.y1 + b.y0) ax.annotate(val, ((b.x0 + b.x1)/2 + x_offset, b.y1 + y_offset))
添加回答
舉報
0/150
提交
取消