2016-11-11 2 views
1

У меня есть таблица в pandas df, у которой есть avg_sp и count1 в виде столбцов. Я построил график, сгруппированный по диапазонам, и я также добавил цикл for для значения сверху.Значение поверх barplot в 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) 

Но я не получаю правильный результат, как показано ниже, она становится смешан с осью х, есть ли способ, чтобы открыть немного в no.s?

enter image description here

enter image description here

я добавил код, который pirsquared предложенное, но это влияет только на верхнюю панель, и другие остаются такими же.

+0

ли вы имеете в виду, как это http://stackoverflow.com/questions/25447700/annotate- баре-с-значения-на-панд-стержневые участки? – lanery

+0

@lanery Я хочу, чтобы значения были наклонены по вертикали – Shubham

+0

Вы можете добавить аргумент 'rotation = 90' в' ax.annotate'. – 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


же раствор с 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 обновил сообщение, и вы всегда рады – piRSquared

+0

в моем ** barplot ** изменение высоты не происходит, его почти касание оси x, даже после запуска отредактированного сообщения, только первая полоса нахождение оставшихся баров остается таким же. – Shubham