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

Viewed 19595

Suppose we read some data into a pandas data frame:

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

The content looks like this:

enter image description here

And then define a function which should give us a horizontal bar chart, where the bar lengths represent values and the bars are labelled with the 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 plot function like this:

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

which gives us the following plot:

enter image description here

Now, what determines the order of the bars?

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

2 Answers
Related