Skip to content

Instantly share code, notes, and snippets.

@arbennett
Created May 22, 2016 23:46

Revisions

  1. arbennett created this gist May 22, 2016.
    26 changes: 26 additions & 0 deletions stacked_bar_subplots.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    """
    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()