Home > Blockchain >  Bar chart using dataframe Matplotlib
Bar chart using dataframe Matplotlib

Time:01-20

I am working on pandas dataframe (data_agg_clust) having below sample data:

           KPIPred
Cluster 
9-11    125.872327
18-20   120.084786
15-17   112.328802
12-14   109.752560
21-23   106.128234

I would like to create a barchart using matplotlib:

import matplotlib.pyplot as plt; plt.rcdefaults()

data_agg_clust.plot.bar(x="Cluster", y="KPIPred", rot=70, title="Predicted Volume of impressions");    
plot.show(block=True);

But unfortunately I got this error:

KeyError                                  Traceback (most recent call last)
~/.pyenv/versions/anaconda3-2021.05/envs/jupiter/lib/python3.10/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3360             try:
-> 3361                 return self._engine.get_loc(casted_key)
   3362             except KeyError as err:

~/.pyenv/versions/anaconda3-2021.05/envs/jupiter/lib/python3.10/site-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

~/.pyenv/versions/anaconda3-2021.05/envs/jupiter/lib/python3.10/site-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Cluster'

The above exception was the direct cause of the following exception

It seems like it didn't recognize "Cluster" column. How can I fix this error?

CodePudding user response:

If your dataframe's index has a name, pandas will distinguish it visually by printing it on a line below the column names. In this case Cluster is the name of your index.

The simplest solution is to leave x alone: it will default to the index if you don't supply a column name:

data_agg_clust.plot.bar(y="KPIPred", rot=70, title="Predicted Volume of impressions")

Another solution is to call data_agg_clust.reset_index to convert the index into a column that can be used for plotting:

data_agg_clust.reset_index().plot.bar(x="Cluster", y="KPIPred", rot=70, title="Predicted Volume of impressions")
  •  Tags:  
  • Related