Created
August 31, 2018 20:17
-
-
Save bhishanpdl/da655765d417c9f1ab38f5275b1b71b5 to your computer and use it in GitHub Desktop.
Pandas plotting using pivot table.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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