How to determine the order of bars in a matplotlib bar chart

前端 未结 2 805
执念已碎
执念已碎 2020-12-18 00:41

Suppose we read some data into a pandas data frame:

data1 = pd.read_csv(\"data.csv\", \"\\t\")

The content looks like this:

2条回答
  •  执笔经年
    2020-12-18 01:07

    If you directly read the key as the index with

    In [12]: df = pd.read_csv('data.csv', '\t', index_col='key')
    
    In [13]: df
    Out[13]: 
         val
    key     
    A    0.1
    B    0.4
    C    0.3
    D    0.5
    E    0.2
    

    you can use ix to get the index in a different order and plot it using df.plot:

    In [14]: df.ix[list('CADFEB')].plot(kind='barh')
    Out[14]: 
    

    barh_example.png

    (Note that F is not given in the data, but you gave it as an example)

提交回复
热议问题