Created
September 8, 2014 21:08
-
-
Save randyzwitch/b71d47e0d380a1a6bef9 to your computer and use it in GitHub Desktop.
Python Seaborn Stacked Bar Chart
This file contains 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
import pandas as pd | |
from matplotlib import pyplot as plt | |
import matplotlib as mpl | |
import seaborn as sns | |
%matplotlib inline | |
#Read in data & create total column | |
stacked_bar_data = pd.read_csv("C:\stacked_bar.csv") | |
stacked_bar_data["total"] = stacked_bar_data.Series1 + stacked_bar_data.Series2 | |
#Set general plot properties | |
sns.set_style("white") | |
sns.set_context({"figure.figsize": (24, 10)}) | |
#Plot 1 - background - "total" (top) series | |
sns.barplot(x = stacked_bar_data.Group, y = stacked_bar_data.total, color = "red") | |
#Plot 2 - overlay - "bottom" series | |
bottom_plot = sns.barplot(x = stacked_bar_data.Group, y = stacked_bar_data.Series1, color = "#0000A3") | |
topbar = plt.Rectangle((0,0),1,1,fc="red", edgecolor = 'none') | |
bottombar = plt.Rectangle((0,0),1,1,fc='#0000A3', edgecolor = 'none') | |
l = plt.legend([bottombar, topbar], ['Bottom Bar', 'Top Bar'], loc=1, ncol = 2, prop={'size':16}) | |
l.draw_frame(False) | |
#Optional code - Make plot look nicer | |
sns.despine(left=True) | |
bottom_plot.set_ylabel("Y-axis label") | |
bottom_plot.set_xlabel("X-axis label") | |
#Set fonts to consistent 16pt size | |
for item in ([bottom_plot.xaxis.label, bottom_plot.yaxis.label] + | |
bottom_plot.get_xticklabels() + bottom_plot.get_yticklabels()): | |
item.set_fontsize(16) |
I uploaded code which would handle N-stacks based on the approach you have outlined here.
https://gist.github.com/extrospective/0f4fe69304184d813f982035d9684452
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great for stack of 2. Seems like it's going to be a bit painful for stack of N.
Am using this as starting point, but seems unreasonably complex that I have to create each subtotal (N, N-1, N-2) and plot those overapping.