简体   繁体   中英

Plotting a histogram for categorical data

I have a dataset with two columns like as follows:

    index   Year
0   5   <2012
1   8   >=2012
2   9   >=2012
3   10  <2012
4   15  <2012
... ... ...
171 387 >=2012
172 390 <2012
173 398 <2012
174 403 >=2012
175 409 <2012

And I would like to plot it in a histogram. I tried with

plt.style.use('ggplot')

df.groupby(['Year'])\
      .Year.count().unstack().plot.bar(legend=True)

plt.show()

but I have got an error: AttributeError: 'CategoricalIndex' object has no attribute 'remove_unused_levels' for

df.groupby(['Year'])\
          .Year.count().unstack().plot.bar(legend=True)

I think this is because I am using categorical values. Any help would be appreciated it.

Try:

plt.style.use('ggplot')
df.groupby(["Year"])["Year"].agg("count").plot.bar();

在此处输入图片说明

Alternatively:

plt.hist(df["Year"]);

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM