我有一個像這樣的資料框字典
{'region': {0: 'R0',1: 'R1',2: 'R2',3: 'R3',4: 'R4',5: 'R5',6: 'R6'},
'DT': {0: 0.765, 1: 0.694, 2: 0.778, 3: 0.694, 4: 0.629, 5: 0.67, 6: 0.668},
'GB': {0: 0.714, 1: 0.741, 2: 0.752, 3: 0.741, 4: 0.683, 5: 0.706, 6: 0.656},
'KNN': {0: 0.625, 1: 0.641, 2: 0.628, 3: 0.641, 4: 0.552, 5: 0.544, 6: 0.578},
'LR': {0: 0.624, 1: 0.662, 2: 0.634, 3: 0.662, 4: 0.581, 5: 0.629, 6: 0.649},
'lstm': {0: 0.803,1: 0.633,2: 0.845,3: 0.668,4: 0.717,5: 0.726,6: 0.674}}
整齊的格式
region DT GB KNN LR lstm
0 R0 0.765 0.714 0.625 0.624 0.803
1 R1 0.694 0.741 0.641 0.662 0.633
2 R2 0.778 0.752 0.628 0.634 0.845
3 R3 0.694 0.741 0.641 0.662 0.668
4 R4 0.629 0.683 0.552 0.581 0.717
5 R5 0.67 0.706 0.544 0.629 0.726
6 R6 0.668 0.656 0.578 0.649 0.674
我想用誤差線繪制堆積條形圖。這個資料框沒有關于標準偏差的資訊,但我有另一個標準偏差資料框。
假設有兩個資料幀均值和標準差
我試過這個代碼
fig, ax = plt.subplots()
width=0.5
clfs=['DT', 'KNN', 'LR', 'GB', 'lstm']
ax.bar(mean_df['region'], mean_df[clfs[0]], width,yerr=std_df[clfs[0]], label=clfs[0])
for i in range(1,5):
ax.bar(mean_df['region'], mean_df[clfs[i]], width,yerr=std_df[clfs[i]], label=clfs[i],bottom=mean_df[clfs[i-1]])
plt.xticks(rotation=90)
plt.legend()
plt.show()
但酒吧沒有正確堆疊。我也在尋找一種在每個條形段上寫入值的方法,以增加繪圖的可讀性

編輯:解決方案是在繪制第三個串列時在底部添加前兩個串列。
fig, ax = plt.subplots()
ax.bar(mean_df['region'], mean_df[clfs[0]], width,yerr=std_df[clfs[0]], label=clfs[0])
ax.bar(mean_df['region'], mean_df[clfs[1]], width,yerr=std_df[clfs[1]], label=clfs[1],bottom=mean_df[clfs[0]])
ax.bar(mean_df['region'], mean_df[clfs[2]], width,yerr=std_df[clfs[2]], label=clfs[2],
bottom=mean_df[clfs[0]] mean_df[clfs[1]])
但我正在尋找一種優雅的方式來做到這一點,以及如何在條形段上寫入值
編輯2:我來到了這個
ax = mean_df.plot(kind='bar', stacked=True, figsize=(8, 6),yerr=std_df, rot=0, xlabel='region', ylabel='DT')
但現在我正在尋找寫文字的方法。我試過這個
for c in ax.containers:
ax.bar_label(c, label_type='center')
但我收到了這個錯誤
AttributeError: 'ErrorbarContainer' object has no attribute 'patches'
編輯 3
這個錯誤是因為yerr=std_df,但我也想保留錯誤欄
uj5u.com熱心網友回復:
- 堆積條形不是呈現資料的理想方式。對于誤差條,堆疊條更難閱讀,可能與給定堆疊內的誤差條以及注釋重疊,這會導致可視化混亂。
- 該問題將發生在
stacked=True或stacked=False,并且它適用于使用
單杠
# plot the dataframe and add yerr ax = pen_mean.plot(kind='barh', stacked=True, figsize=(9, 6), rot=0, xerr=pen_std) # move the legend ax.legend(bbox_to_anchor=(1, 1.02), loc='upper left') # iterate through every other container; the even containers are ErrorbarContainer for c in ax.containers[1::2]: # add the annotation ax.bar_label(c, label_type='center')
Axes.bar和Axes.errorbar- BarContainer 物件位于偶數索引處,可以使用
ax.containers[0::2]
data = pen_mean cols = pen_mean.columns rows = pen_mean.index # Get some pastel shades for the colors colors = ['tab:blue', 'tab:green'] n_rows = len(data) index = np.arange(len(cols)) bar_width = 0.4 # Initialize the vertical-offset for the stacked bar chart. y_offset = np.zeros(len(cols)) # Plot bars and create text labels for the table fig, ax = plt.subplots(figsize=(8, 5)) for i, row in enumerate(rows): ax.bar(cols, data.loc[row], bar_width, bottom=y_offset, color=colors[i]) ax.errorbar(cols, y_offset data.loc[row], pen_std.loc[row], color='k', ls='none') y_offset = y_offset data.loc[row] # note the order of the container objects is different for c in ax.containers[0::2]: ax.bar_label(c, label_type='center') plt.show()
海底酒吧
- 帶有默認值的 seaborn 條形圖
ci=True不會ErrorbarContainer objects在containers.
sns.catplot和kind='bar'- 有關注釋 seaborn 圖形級條形圖的其他示例,請參閱此
sns.barplotfig = plt.figure(figsize=(9, 6)) p = sns.barplot(data=pen, x='sex', y='bill_depth_mm', hue='species') p.legend(bbox_to_anchor=(1, 1.02), loc='upper left') for c in p.containers: # add the annotation p.bar_label(c, label_type='center')
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/383671.html標籤:Python 熊猫 matplotlib 注释 条形图
上一篇:如何使用matplotlib填充或遮蔽Python中堆積條形圖中兩個對應點之間的區域?
下一篇:保持形狀內的2D插值
- BarContainer 物件位于偶數索引處,可以使用
