1

I'm plotting a pandas dataframe group by a column, and would like to visualize it as a plot bar with the value at the top of it. Most of the examples I found are not straightforward and use matplotlib directly. What is the simple way to show the row value at the top of the bar?

My dataframe looks like:


             |No. of Files|
|reload_date|
03/30/2019             86
03/31/2019             96
04/01/2019             78
04/11/2019            320
04/12/2019            357
04/22/2019            460
04/27/2019            498
04/28/2019            565
04/29/2019            913
04/30/2019           1010
05/01/2019           1428
05/27/2019           1419
05/28/2019           1477
05/29/2019           1655

This works perflecty to plot the bar, but it is missing the value at the top.

files_per_date = data_set[['FileName','reload_date']]
count_files = files_per_date.groupby(['reload_date']).count()
ax = plotme.plot.bar(rot=0, subplots=True)


plt.title("Files Reloaded per Day")
plt.xlabel("Date of Reloading")
plt.ylabel("Number of Files")

enter image description here I tried to loop the sublots using this example:

 for p in axtest.patches:
     axtest.annotate('{:.2E}'.format(Decimal(str(p.get_height()))), (p.get_x(), p.get_height()))
 plt.tight_layout()

I expect to have a numerical value at the top, like below enter image description here but instead got this error:

  File "C:\Users\XXXX\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 140, in 
pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 162, in 
pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in 
pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1500, in 
pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'reload_date'
Sora
  • 95
  • 1
  • 10

1 Answers1

3

Try adding this nested for loop:

axes = plotme.plot.bar(rot=0, subplots=True)

plt.title("Files Reloaded per Day")
plt.xlabel("Date of Reloading")
plt.ylabel("Number of Files")

for ax in axes:
    for p in ax.patches:
        height = p.get_height()
        x, y = p.get_xy() 
        ax.annotate('{}'.format(height), (x, y + height))
Chris Adams
  • 18,389
  • 4
  • 22
  • 39