【问题标题】:Labeling into Bars in Barchart with pyplot and Pandas fails使用 pyplot 和 Pandas 在 Barchart 中标记条形失败
【发布时间】: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


    【解决方案1】:

    你可以用xy来决定注解的位置。您使用value - 47000 为每个国家/地区设置不同的值。这也带来了一些负值(在图表之外)。要将它们全部显示在栏的开头,您可以使用固定值,例如:

    xy=(1000, index - 0.10)
    

    或大于最小值的值(在这种情况下,它将位于柱的末尾):

     xy=(value - 21000, index - 0.10)
    

    【讨论】:

    • @user8270077 不客气。不要忘记为您认为有用的答案投票,并接受您认为最好的答案。快乐编码:)
    【解决方案2】:

    不确定您要查找的内容,但请尝试更改行: plt.annotate(label, xy=(value - 47000, index - 0.10), color='white')

    到: plt.text(value, index, label, ha='right', va='center')

    【讨论】:

      【解决方案3】:

      您的代码的问题是您要减去的 x 值,即 47000(它大于最小值),只需将该数字减少到 47(或大于最小值的任何数字),它就会起作用。如果您有白色背景,也可以将文本颜色更改为黑色

      % %matplotlib inline
      import matplotlib.pyplot as plt
      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 - 47, index - 0.10), color='black')
      
      plt.show()
      

      替代方法:

      % matplotlib inline
      import matplotlib.pyplot as plt
      ax = df.plot(kind='barh', figsize=(15,10),color="steelblue", fontsize=13);
      
      ax.set_title("Top 15 Conuntries Contributing to the Immigration to Canada between 1980 - 2013", fontsize=18)
      ax.set_xlabel("Number of Immigrants", fontsize=18);
      
      for i in ax.patches:
          # get_width pulls left or right; get_y pushes up or down
          ax.text(i.get_width()+.1, i.get_y()+.31, \
                  str(round((i.get_width()), 2)), fontsize=10, color='black')
      

      【讨论】:

      • 如果您认为这将帮助他人并且值得,您可以投票吗?
      猜你喜欢
      • 2019-08-02
      • 1970-01-01
      • 1970-01-01
      • 2017-08-27
      • 2012-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多