I would like to change labels to bar plots from groupby
, possibly by mapping a label to classes instead of changing values in the dataset.
In matplotlib it is possible to style the classes : https://www.pythoncharts.com/matplotlib/grouped-bar-charts-matplotlib/
can I do something similar in pandas ?
What I mean is this:
dt = pd.DataFrame(np.random.randint(0,3,size=(10, 3)), columns=list('ABC'))
# I create a multi_index object and plot
dt.groupby('A')['B'].value_counts().unstack(0).plot(kind='bar')
See the legend of the classes under B ?
Instead of 0, 1, 2
, I would like to pass names, like 'good', 'bad', average'
.
I was trying to look at ways to map a function or dictionary as labels, but not working.
e.g.
dt.groupby('A')['B'].value_counts().unstack().plot(kind='bar', label = {0:'a', 1:'2', 2:'d'})
does nothing.
Alternatively I was trying to change values of the index in a multi_index, but it is cumbersome and could not find a way to do it without hitting errors (I tried with loc
, apply
, reset_index
- could not find a way).
Eventually I would set on matplotlib, but for my purpose, a oneliner would be ideal.
Just would like to adjust those labels on classes.
Can you show a synthetic way ?
Is it a good practice to alter values in a df ? I would prefer a mapping function on plotting level.