python 条形图图注怎么集中注意力_如何用每个条形图的总和(Matplotlib)注释堆积条形图?...
我有一個(gè)分組條形圖,每個(gè)條形圖都是堆疊的。在
我已經(jīng)用單獨(dú)的值注釋了堆棧的每個(gè)部分,現(xiàn)在我想將這些值相加,并注釋每個(gè)條的總值(高度)。我希望這個(gè)注釋在每個(gè)條的頂部。在
這是我使用的兩個(gè)數(shù)據(jù)幀之一:df_title = pd.DataFrame(index=['F','M'],
data={'<10':[2.064897, 1.573255], '10-12':[3.933137, 4.326450], '13-17':[9.242871, 16.715831],
'18-24':[10.226155, 12.487709], '18-24':[8.161259, 10.717797], '35-44':[5.801377, 4.916421],
'45-54':[3.539823, 2.851524], '55+':[1.671583, 1.769912]})
在繪圖之前,我將兩個(gè)數(shù)據(jù)幀(df_title和df_comps)轉(zhuǎn)換成numpy數(shù)組。在
^{pr2}$
以下是完整代碼:df_title
df_comps
len = df_title.shape[1]
df_title_concat = np.concatenate((np.zeros((len,1)), df_title.T.values), axis=1)
df_comps_concat = np.concatenate((np.zeros((len,1)), df_comps.T.values), axis=1)
fig = plt.figure(figsize=(20,10))
ax = plt.subplot()
title_colors = ['skyblue', 'royalblue']
comps_colors = ['lightgoldenrodyellow', 'orange']
for i in range(1,3):
for j in list(range(0, df_title.shape[1]-1)):
j += 1
ax_1 = ax.bar(j, df_title_concat[j,i], width=-0.4, bottom=np.sum(df_title_concat[j,:i]), color = title_colors[i-1],
edgecolor='black', linewidth=3, align='edge')
for p in ax_1.patches:
width, height = p.get_width(), p.get_height()
x, y = p.get_xy()
if height > 2:
ax.annotate('{:.2f}%'.format(height), (p.get_x()+0.875*width, p.get_y()+.4*height),
fontsize=16, fontweight='bold', color='black')
ax_2 = ax.bar(j, df_comps_concat[j,i], width=0.4, bottom=np.sum(df_comps_concat[j,:i]), color = comps_colors[i-1],
edgecolor='black', linewidth=3, align='edge')
for p in ax_2.patches:
width, height = p.get_width(), p.get_height()
x, y = p.get_xy()
if height > 2:
ax.annotate('{:.2f}%'.format(height), (p.get_x()+0.15*width, p.get_y()+.4*height),
fontsize=16, fontweight='bold', color='black')
{1美元^
總結(jié)
以上是生活随笔為你收集整理的python 条形图图注怎么集中注意力_如何用每个条形图的总和(Matplotlib)注释堆积条形图?...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中shift_python对
- 下一篇: 如何把二进制数化成十进制数_二进制数按位