【问题标题】:Pandas Plot, arrange bar value from high to low, and colour Individual bar separatelyPandas Plot,从高到低排列条形值,并分别为单个条形着色
【发布时间】:2021-05-29 15:37:30
【问题描述】:

我有一个系列如下:

Country
India                                                   691904
China                                                   659962
United Kingdom of Great Britain and Northern Ireland    551500
Philippines                                             511391
Pakistan                                                241600
United States of America                                241122
Iran (Islamic Republic of)                              175923
Sri Lanka                                               148358
Republic of Korea                                       142581

绘制水平条形图后,图x轴从低到高排列

问: 1) 如何将 y 轴从高到低排列?

2) 有没有更简单的方法将除最高值(在本例中为印度)之外的所有条形都涂成灰色,而无需输入所有国家/地区名称?

谢谢!

ax = top15.plot(kind='barh',
             figsize=(20,10),
               color = {'red':'India'})

ax.set_title('Immigration from 1980 - 2013')
ax.set_xlabel('Total Number')

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    首先使用sort_values()

    top15=top15.sort_values(ascending=True)
    

    最后:

    color=['0.8','0.8','0.8','0.8','0.8','0.8','0.8','0.8','r']
    #created a list of colors
    
    ax = top15.plot(kind='barh',
                 figsize=(20,10),
                   color = color)
    
    ax.set_title('Immigration from 1980 - 2013')
    ax.set_xlabel('Total Number')
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-30
      • 2018-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多