Data Scientist @ RMOTR

# Stacked Bar Plot Examples

Last updated: June 19th, 2019

# Stacked bar plot examples¶

In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

In [2]:
df = pd.DataFrame([[3,2,3,4,5], [3,2,3,4,5], [4,3,2,1,2], [4,3,2,1,2]])
df.columns = ['DevType1', 'DevType2', 'DevType3', 'DevType4', 'DevType5']
df.index = pd.Series(['0-2 years', '2-4 years', '4-6 years', '6-8 years'], name='YearsCoding')

df

Out[2]:
DevType1 DevType2 DevType3 DevType4 DevType5
YearsCoding
0-2 years 3 2 3 4 5
2-4 years 3 2 3 4 5
4-6 years 4 3 2 1 2
6-8 years 4 3 2 1 2

We'll get a sum of our values by row. This will help us to verify our plot later.

In [3]:
df.sum(axis=1)

Out[3]:
YearsCoding
0-2 years    17
2-4 years    17
4-6 years    12
6-8 years    12
dtype: int64

## Using pandas¶

In [4]:
fig, ax = plt.subplots(figsize=(16,8))

df.plot(kind='bar', stacked=True, ax=ax)

Out[4]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f30b58226d8>
In [5]:
fig, ax = plt.subplots(figsize=(16,8))

df.plot(kind='barh', stacked=True, ax=ax)

Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f30b3744a20>

## Using matplotlib¶

In [6]:
index = np.arange(df.index.size)
bar_size = 0.4
plt.figure(figsize=(16,8))

for i, x in enumerate(df.columns):
if i==0:
plt.bar(x=index, height=df.iloc[:,i], width=bar_size)
else:
# we should define 'bottom' to make our stack
plt.bar(x=index, height=df.iloc[:,i], width=bar_size, bottom=df.iloc[:,:i].sum(axis=1))

plt.title('DevType per YearsCoding')
plt.ylabel('Count')
plt.ylim(0, df.sum(axis=1).max() + 1)
plt.xticks(index, df.index, rotation=0)
plt.legend(df.columns)

Out[6]:
<matplotlib.legend.Legend at 0x7f30b3651a20>
In [7]:
index = np.arange(df.index.size)
bar_size = 0.4
plt.figure(figsize=(16,8))

for i, x in enumerate(df.columns):
if i==0:
plt.barh(y=index, width=df.iloc[:,i], height=bar_size)
else:
# we should define 'left' to make our stack
plt.barh(y=index, width=df.iloc[:,i], height=bar_size, left=df.iloc[:,:i].sum(axis=1))

plt.title('DevType per YearsCoding')
plt.xlabel('Count')
plt.xlim(0, df.sum(axis=1).max() + 1)
plt.yticks(index, df.index, rotation=0)
plt.legend(df.columns)

Out[7]:
<matplotlib.legend.Legend at 0x7f30b35febe0>