我的資料:
names_col = ['Count','Freq']
dat = [['Matching', 56935],['Mismatching', 100587]]
plot_df = pd.DataFrame(data=dat,columns=names_col)
我嘗試使用顯示值繪制堆疊貓圖,這是我的代碼:
plt.figure(figsize=(16,9))
p=plot_df.set_index('Count').T.plot(kind='bar', stacked=True)
p.bar_label(p.containers[0])
p.bar_label(p.containers[1])
plt.show();
首先,圖形的輸出不是大小(16,9),有什么問題?第二個圖顯示值為:

取而代之的是matching- 56935(這里可以)和mismatching- 100587 的值,繪圖顯示總計(157522)。我如何訪問和展示也Mismatching有價值?
uj5u.com熱心網友回復:
您可以設定figsize為繪圖的引數。然后為您的每個容器添加條形標簽和您自己的文本:
p=plot_df.set_index('Count').T.plot(kind='bar', stacked=True, figsize=(16,9))
for x in p.containers:
p.bar_label(x)
p.text(0, x[0].get_y() x[0].get_height()*0.5, x.datavalues[0], ha='center', color='w', weight='bold')
plt.show()
輸出:

uj5u.com熱心網友回復:
- 使用
- 這是一個包含多個組的更全面的示例
- 另一個答案沒有放置第二組條形的中間注釋。
# test data data = {'Matching': [56935, 17610], 'Mismatching': [100587, 13794], 'Test': [33139, 23567]} df = pd.DataFrame(data=data, index=['Freq', 'Freq2']) axe = df.plot(kind='bar', stacked=True, figsize=(16,9), rot=0) for x in axe.containers: axe.bar_label(x, label_type='edge', weight='bold') axe.bar_label(x, label_type='center', weight='bold', color='white')
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/376713.html標籤:Python 熊猫 matplotlib 注释 条形图
上一篇:HTML右對齊最后一列
- 這是一個包含多個組的更全面的示例
