Skip to content

Instantly share code, notes, and snippets.

@arbennett
Created May 22, 2016 23:46
Show Gist options
  • Save arbennett/a02fbc95b66d89d89779b72364d8e35f to your computer and use it in GitHub Desktop.
Save arbennett/a02fbc95b66d89d89779b72364d8e35f to your computer and use it in GitHub Desktop.
Script to do multiple stacked bar subplots
"""
Script to do multiple stacked bar subplots
"""
import matplotlib.pyplot as plt
data = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]]
colors=['r','g','b','y']
n_bars = len(data)
fig, ax = plt.subplots(1,n_bars)
# Go through each separate bar subplot
for bar in range(n_bars):
n_segments = len(data[bar])
segment_data = data[bar]
offset = 0
# Create the subplot
for segment in range(n_segments):
# Select the new subplot
plt.subplot(1,n_bars, bar)
plt.bar(1, segment_data[segment], bottom=offset, color=colors[segment])
# Extend offset by current data amount so that stacking works properly
offset += segment_data[segment]
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment