用熊猫在酒吧的顶部显示价值

时间:2019-06-14 09:02:48

标签: python pandas matplotlib pandas-groupby

我正在按列绘制pandas数据框组,并希望将其可视化为绘制栏,其值位于顶部。我发现的大多数示例都不是简单明了的,而是直接使用matplotlib。在栏顶部显示行值的简单方法是什么?

我的数据框如下:


             |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

这样可以很好地绘制条形图,但是缺少顶部的值。

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 我尝试使用以下示例循环子批处理:

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

我希望顶部有一个数值,如下所示 enter image description here 但是却出现此错误:

  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'

1 个答案:

答案 0 :(得分:1)

尝试添加此嵌套的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))