I am trying to plot a bar chart of a pandas
data frame that is the result of two group bys.
In particular, my data frame looks exactly like the output from another SO post's answer (https://stackoverflow.com/a/23377155/7243972):
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
np.random.seed(0)
df = pd.DataFrame({'state': ['CA', 'WA', 'CO', 'AZ'] * 3,
'office_id': list(range(1, 7)) * 2,
'sales': [np.random.randint(100000, 999999) for _ in range(12)]})
state_office = df.groupby(['state', 'office_id']).agg({'sales': 'sum'})
state = df.groupby(['state']).agg({'sales': 'sum'})
results = state_office.div(state, level='state') * 100
I would like to plot results
so that each state
is a different color and the office_id
is on the x-axis. This is so that each office_id
is grouped together and they can be easily compared.
I've tried adjusting the plot from results['sales'].plot.bar()
, but I am struggling.