2016-11-11 7 views
1

私はテーブルとしてavg_spとcount1を列として持つpandas dfを持っています。私は範囲でグループ化された棒グラフをプロットし、また上の値のforループを追加しました。Pythonの棒グラフの上の値

plt.figure(figsize=(12, 6)) 
df2 = df.groupby(pd.cut(df['avg_sp'], range(0, 110,10))).sum() ['count1'].plot(kind='bar') 
plt.xlabel('avg_sp') 
plt.ylabel('browse count') 

for p in df2.patches: 
    df2.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.005),rotation=90) 

しかし、以下に示すように、私はそれがx軸との混合なってきて、正しい結果を取得していない午前、少しno.sを起動する方法はありますか?

enter image description here

enter image description here

私が提案pirsquaredコードを追加し、それが唯一のトップバーに影響を及ぼしている、と他は同じまま。

+0

が欲しいところそれを得るためにビットを設定しますが、このように意味しますかhttp://stackoverflow.com/questions/25447700/annotate-パンダ・バリュー・オン・パンダ・バー・プロット? – lanery

+0

@lanery私は、値を垂直方向に傾けたいです。 – Shubham

+0

'ax.annotate'に引数' rotation = 90'を追加することができます。 – lanery

答えて

3

はシリーズs

s = pd.Series(np.random.randn(10000)) 
s.hist() 

enter image description here


from matplotlib docs

def autolabel(rects): 
    # attach some text labels 
    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') 

ax = s.hist() 
for c in ax.containers: 
    autolabel(c) 

enter image description here

012を検討します ax.patches

ax = s.hist() 
for rect in ax.patches: 
    height = rect.get_height() 
    ax.text(rect.get_x() + rect.get_width()/2., 1.05*height, 
      '%d' % int(height), 
      ha='center', va='bottom') 

enter image description here


rotated labels docs

ax = s.hist() 
for rect in ax.patches: 
    height = rect.get_height() 
    ax.text(rect.get_x() + rect.get_width()/2., 1.05*height, 
      '%d' % int(height), 
      ha='center', va='bottom', rotation=90) 

enter image description here

同じ溶液私は、高さとめちゃめちゃ

は私が

ax = s.hist() 
for rect in ax.patches: 
    height = rect.get_height() 
    ax.text(rect.get_x() + rect.get_width()/2., 1.01*height+100, 
      '%d' % int(height), 
      ha='center', va='bottom', rotation=90) 

enter image description here

+0

とrotation = 90もこれで動作しますか?ところで、ありがとう – Shubham

+0

@SRingne更新された投稿と私の** barplot **で – piRSquared

+0

を歓迎しています**高さの変更は、編集された投稿を実行した後でも、x軸にほとんど触れていません 最初のバーは影響を受ける、残りのバーの距離は同じです – Shubham