【发布时间】:2018-12-07 01:22:05
【问题描述】:
我的目标是创建一个条形图并通过在其中插入它们对应的值来注释条形图。
我的代码出了点问题,但我不知道是什么:
我的数据:
top_15
Total
Country
China 228100
India 183289
Pakistan 92869
Philippines 81537
United Kingdom of Great Britain and Northern Ireland 60356
Republic of Korea 49094
Iran (Islamic Republic of) 45713
United States of America 38151
Sri Lanka 35156
Romania 33823
Russian Federation 28283
France 25550
Afghanistan 21941
Ukraine 21113
Morocco 21092
我的代码:
top_15.plot(kind='barh', figsize=(10, 10), color='steelblue')
plt.xlabel('Number of Immigrants')
plt.title('Top 15 Conuntries Contributing to the Immigration to Canada between 1980 - 2013')
# annotate value labels to each country
for index, value in enumerate(top_15.loc[:,'Total']):
label = format(int(value), ',') # format int with commas
# place text at the end of bar (subtracting 47000 from x, and 0.1 from y to make it fit within the bar)
plt.annotate(label, xy=(value - 47000, index - 0.10), color='white')
plt.show()
输出:
【问题讨论】:
标签: python-3.x pandas matplotlib label bar-chart