I have an excel file that contains information about some data with two columns, one describing the object and one how many that object appears, for example:
house 5
car 3
boat 2
I want to create an histogram, so I created a pandas df
X = pandas.read_table("myfile.txt")
categories = pandas.DataFrame({
"object": X["object"],
"total": X["total"]
})
Next I plot it by:
categories.total.to_frame("objects").query("objects> 2").sort_values("objects", ascending=False).plot.bar(width=1)
This works, but I would like to show, on the x axis, the name of the objects, right now it just enumerates the objects. How do I do that?
答案 0 :(得分:2)
Pandas uses index per default when plotting x-axis, so you can easily set it:
In [27]: categories.set_index('object').query("total > 2").sort_values('total', ascending=False).plot.bar(rot=0, width=1)
Out[27]: <matplotlib.axes._subplots.AxesSubplot at 0xabace48>
alternatively you can specify x axis explicitly:
In [31]: categories.query("total > 2").sort_values('total', ascending=False).plot.bar(x='object', rot=0, width=1)
Out[31]: <matplotlib.axes._subplots.AxesSubplot at 0xa9a64e0>