import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import random
# data
random.seed(365)
data = {'Year': [random.choice([2016, 2017]) for _ in range(1000)],
'Region': [random.choice(['A', 'B']) for _ in range(1000)],
'Quarter': [random.choice([1, 2, 3, 4]) for _ in range(1000)],
'Value': [np.random.randint(10) for _ in range(1000)]}
# dataframe
df = pd.DataFrame(data)
# groupby
gb = df.groupby(['Region', 'Year', 'Quarter'])['Value'].sum().unstack()
Quarter 1 2 3 4
Region Year
A 2016 306 279 221 261
2017 209 250 292 415
B 2016 262 296 261 260
2017 209 349 272 315
# plot
gb.plot.bar(stacked=True)
plt.legend(title='Quarter', loc='center left', bbox_to_anchor=(1, 0.5))
plt.show()