How to determine the order of bars in the matplotlib histogram

Suppose we read some data in a pandas data frame:

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

The content is as follows:

enter image description here

And then define a function that should give us a horizontal histogram, where the stroke lengths represent the values ​​and the bars are marked with keys.

 def barchart(data, labels): pos = arange(len(data))+.5 # the bar centers on the y axis barh(pos, data, align='center', height=0.25) yticks(pos, labels) 

Then we call the chart function as follows:

 barchart(data1["val"], data1["key"]) 

which gives us the following graph:

enter image description here

Now, what determines the order of the bars?

Suppose we want the bars to be in a special order, say [C, A, D, F, E, B] , how can we ensure this?

+6
source share
2 answers

I modified the original version of barchart. To indicate the order of the bars, I use the index set through column ii:

 import numpy as np import pandas as pd import matplotlib.pyplot as plt def barchart(data, labels): pos = np.arange(len(data)) + 0.5 # the bar centers on the y axis plt.barh(pos, data.sort_index(), align='center', height=0.25) plt.yticks(pos, labels.sort_index()) data1 = pd.DataFrame({'key': list('ABCDE'), 'val': np.random.randn(5)}) new_keys = list('EDACB') data1['ii'] = [new_keys.index(x) for x in data1.key] data1 = data1.set_index('ii') barchart(data1["val"], data1["key"]) plt.show() 
+3
source

If you directly read the key as an index using

 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 build it with df.plot :

 In [14]: df.ix[list('CADFEB')].plot(kind='barh') Out[14]: <matplotlib.axes._subplots.AxesSubplot at 0x530fa90> 

barh_example.png

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

+8
source

All Articles