pandas 在熊猫顶部带有标签名称和值的条形图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51676506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:53:17  来源:igfitidea点击:

Bar chart with label name and value on top in pandas

pythonpandas

提问by M Hossain

I have two columns where i used groupby option create a df called output_duration_per_device such as

我有两列,我使用 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

I can make a bar chat and find like this

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

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')

which gives me enter image description here

这给了我 在此处输入图片说明

but i want like below enter image description here

但我想要像下面这样 在此处输入图片说明

please help me

请帮我

回答by user3483203

Using plotand annotating via height (I would recommend fiddling with the spacing):

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

在此处输入图片说明

回答by Masud Rumii

"""

"""

Barchart

柱状图

A bar plot with errorbars and height labels on individual bars """

条形图在单个条形上带有误差条和高度标签 """

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

条形图输出

The following code is collected from Matplotlib official website. Please take a look. click here

以下代码摘自 Matplotlib 官网。请看一下。点击这里