我的数据框中有df
的数据,其中包含3列(CODE,AGE,COUNT)
import pandas as pd
data = {'CODE': ['A', 'B', 'N', 'k', 'A', 'A', 'Z', 'L', 'O', 'B', 'Z'],
'AGE': [1, 2, 3, 4, 5, 6, 40, 42, 48, 65, 65],
'COUNT': [1800, 4000, 2000, 6500, 3500, 5000, 4, 10, 10, 8, 8]}
df = pd.DataFrame(df)
我需要将数据绘制为条形图,其中X轴为AGE,Y轴为COUNT。还需要在图中每个条形的顶部标记诸如A,B,N,Z等代码。我们如何在熊猫的每个小节中提及CODE?
答案 0 :(得分:0)
IIUC,您想要这样的东西:
ax = df.plot.bar(x='AGE', y='COUNT', logy=True)
for n, i in enumerate(ax.patches):
ax.annotate(df.iloc[n, df.columns.get_loc('CODE')], (i.get_x() + i.get_width() / 2., i.get_height()),
ha='center', va='center', fontsize=11, color='black', xytext=(0, 5),
textcoords='offset points')
#Make space for labels
_ = ax.set_ylim(0, df['COUNT'].max()+10000)
输出: