Profile picture

Data Scientist @ RMOTR

Stacked Bar Plot Examples

Last updated: May 10th, 20192019-05-10Project preview

Stacked bar plot examples


In [26]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
In [7]:
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[7]:
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 [8]:
df.sum(axis=1)
Out[8]:
YearsCoding
0-2 years    17
2-4 years    17
4-6 years    12
6-8 years    12
dtype: int64

Using pandas

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

df.plot(kind='bar', stacked=True, ax=ax)
Out[24]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fa16da329b0>
In [25]:
fig, ax = plt.subplots(figsize=(16,8))

df.plot(kind='barh', stacked=True, ax=ax)
Out[25]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fa16d339f98>

Using matplotlib

In [9]:
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[9]:
<matplotlib.legend.Legend at 0x7fa16df66a58>
In [10]:
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[10]:
<matplotlib.legend.Legend at 0x7fa16df58cf8>
Notebooks AI
Notebooks AI Profile20060