条形图,标签名称和值位于熊猫的顶部

时间:2018-08-03 16:11:31

标签: python pandas

我在两列中使用groupby选项创建了一个名为output_duration_per_device的df,例如

output_duration_per_device=s3_dataset.groupby('DeviceType')['Output_media_duration'].sum().reset_index(name ='format_duration')

output_duration_per_device
DeviceType           format_duration
0   Alchemist          8.166905e+06
1   CaptionMaker       1.310864e+07
2   Elemental          1.818089e+07
3   EncodingCloud      0.000000e+00
4   FfMpeg             5.258470e+07
5   FlipFactory        4.747456e+02
6   Rhozet             6.263442e+08
7   Tachyon            4.827463e+06

我可以进行酒吧聊天并找到这样的

output_duration_per_device=s3_dataset.groupby('DeviceType')['Output_media_duration'].sum().reset_index(name ='Device_duration').plot(kind ='bar', figsize=(10,7), fontsize=13)
output_duration_per_device.set_alpha(0.8)
output_duration_per_device.set_title('DeviceType Output Media duration')
output_duration_per_device.set_xlabel('DeviceType')
plt.ylabel('Output_media_duration')

这给了我 enter image description here

但是我想要下面 enter image description here

请帮助我

2 个答案:

答案 0 :(得分:2)

使用 plot 并通过高度进行注释(我建议摆弄一下间距):

from decimal import Decimal
ax = df.plot(x='DeviceType', y='format_duration', kind='bar')
for p in ax.patches:
    ax.annotate('{:.2E}'.format(Decimal(str(p.get_height()))), (p.get_x(), p.get_height()))
plt.tight_layout()
plt.show()

enter image description here

答案 1 :(得分:1)

“”“

Barchart

在各个条形图上带有误差线和高度标签的条形图 “”“

import numpy as np
import matplotlib.pyplot as plt

N = 5
men_means = (20, 35, 30, 35, 27)
men_std = (2, 3, 4, 1, 2)

ind = np.arange(N)  # the x locations for the groups
width = 0.35       # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(ind, men_means, width, color='r', yerr=men_std)

women_means = (25, 32, 34, 20, 25)
women_std = (3, 5, 2, 3, 3)
rects2 = ax.bar(ind + width, women_means, width, color='y', yerr=women_std)

# add some text for labels, title and axes ticks
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind + width / 2)
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))

ax.legend((rects1[0], rects2[0]), ('Men', 'Women'))


def autolabel(rects):
    """
    Attach a text label above each bar displaying its height
    """
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
                '%d' % int(height),
                ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)

plt.show()

Bar Chart output

以下代码是从Matplotlib官方网站收集的。请看一下。 click here