2014-11-05 137 views
1

我有我ploting像这样一个熊猫DataFrameGroupBy对象...添加datalabels - matplotlib条形图

grouped.sum().plot(kind='bar') 

这给...

enter image description here

,但我想更多的东西像这样...

enter image description here

我只想知道如何将数据标签添加到每个列和可能的表中?

+0

什么版本的熊猫? – Anzel 2014-11-05 15:57:38

+0

版本'0.15.0' – 2014-11-05 16:08:56

回答

3

要在柱状图上添加值标签,你可以通过在每个索引中的列循环和使用text显示值,像这样:

df = DataFrame(rand(3,2), columns=['A', 'B']) 
ax = df.plot(table=True, kind='bar', title='Random') 
for i, each in enumerate(df.index): 
    for col in df.columns: 
     y = df.ix[each][col] 
     ax.text(i, y, y) 

您可能需要调整文本标签COORDS有一个更好的定位等


要显示网格表,pandas具有0.14+表支持。

你可以阅读更多关于Plotting table HERE

与matplotlib表现在绘图用表关键字支持DataFrame.plot() 和Series.plot()。 table关键字可以接受 bool,DataFrame或Series。绘制表格的简单方法是 指定table = True。数据将被转置为符合matplotlib的默认布局 。

fig, ax = plt.subplots(1, 1) 
df = DataFrame(rand(5, 3), columns=['a', 'b', 'c']) 
ax.get_xaxis().set_visible(False) # Hide Ticks 
df.plot(table=True, ax=ax) 

此外,您还可以通过不同的数据帧或系列的表关键字。 数据将按打印方式显示(不自动转换 )。如果需要,应该按照以下示例手动调换 。

fig, ax = plt.subplots(1, 1) 
ax.get_xaxis().set_visible(False) # Hide Ticks 
df.plot(table=np.round(df.T, 2), ax=ax)