Skip to content

Instantly share code, notes, and snippets.

@bhishanpdl
Created August 31, 2018 20:17
Show Gist options
  • Save bhishanpdl/da655765d417c9f1ab38f5275b1b71b5 to your computer and use it in GitHub Desktop.
Save bhishanpdl/da655765d417c9f1ab38f5275b1b71b5 to your computer and use it in GitHub Desktop.
Pandas plotting using pivot table.
data = [[2000, 2000, 2000, 2001, 2001, 2001, 2002, 2002, 2002],
['Jan', 'Feb', 'Mar', 'Jan', 'Feb', 'Mar', 'Jan', 'Feb', 'Mar'],
[1, 2, 3, 4, 5, 6, 7, 8, 9]]
rows = list(zip(data[0], data[1], data[2]))
headers = ['Year', 'Month', 'Value']
df = pd.DataFrame(rows, columns=headers)
pivot_df = df.pivot(index='Year', columns='Month', values='Value')
#Note: .loc[:,['Jan','Feb', 'Mar']] is used here to rearrange the layer ordering
colors = ["#006D2C", "#31A354","#74C476"]
pivot_df.loc[:,['Jan','Feb', 'Mar']].plot.bar(stacked=True, color=colors, figsize=(10,7))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment