1

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

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

M Hossain
  • 77
  • 1
  • 2
  • 10

2 Answers2

8

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

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

user3483203
  • 50,081
  • 9
  • 65
  • 94
2

"""

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

  • 5
    I think it would be more helpful to adapt this answer to the question, instead of just copy-pasting from the matplotlib website. The code you've provided would work for this question, so it shouldn't take many changes to adapt it! – user3483203 Aug 03 '18 at 16:58
  • Except OP specifies he wants to do this using Pandas? – Tim Kirkwood Oct 02 '21 at 10:33