Suppose we read some data into a pandas data frame:
data1 = pd.read_csv("data.csv", "")
The content looks like this:
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:
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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…