10

I want to create a stacked barplot with 3 bars on top of each other. I managed to do this for a 2-bar stacking, but I can't add the 3rd one, any ideas?

I will add some simple example code to show you what I mean:

from matplotlib import pyplot as plt

data1 = [100,120,140]
data2 = [150,120,190]

f, (ax1, ax2) = plt.subplots(1, 2, figsize=(10,5))

## Absolute count

ax1.bar(range(len(data1)), data1, label='data 1', alpha=0.5, color='b')
ax1.bar(range(len(data2)), data2, bottom=data1, label='data 2', alpha=0.5, color='r')
plt.sca(ax1)
plt.xticks([0.4, 1.4, 2.4],  ['category 1', 'category 2', 'category 3'])
ax1.set_ylabel("Count")
ax1.set_xlabel("")
plt.legend(loc='upper left')

## Percent

totals = [i + j for i,j in zip(data1, data2)]
data1_rel = [i / j * 100 for  i,j in zip(data1, totals)]
data2_rel = [i / j * 100 for  i,j in zip(data2, totals)]

ax2.bar(range(len(data1_rel)), data1_rel, alpha=0.5, color='b')
ax2.bar(range(len(data2_rel)), data2_rel, bottom=data1_rel, alpha=0.5, color='r')
plt.sca(ax2)
plt.xticks([0.4, 1.4, 2.4],  ['category 1', 'category 2', 'category 3'])
ax2.set_ylabel("Percentage")
ax2.set_xlabel("")

plt.show()

enter image description here

Now, let's say I want to add, e.g., data3 = [100,150,130] Intuitively, I would do it like this

ax1.bar(range(len(data3)), data3, bottom=data1+data2, label='data 3', alpha=0.5, color='g')

However, this unfortunately doesn't add the 3rd bar.

4 Answers4

10

Should do: ax1.bar(range(len(data3)), data3, bottom=np.array(data1)+np.array(data2), label='data 3', alpha=0.5, color='g'): enter image description here

And, may be a preferred way. it can be very elegantly handled by pandas in just a few lines:

In [17]:

import pandas as pd
df=pd.DataFrame({'data1':data1, 'data2':data2, 'data3':data3})
df.plot(kind='bar', stacked=True)
Out[17]:
<matplotlib.axes.AxesSubplot at 0x108f2b050> 

enter image description here

CT Zhu
  • 52,648
  • 17
  • 120
  • 133
2

An option if you want to use the pandas plotting library instead of matplotlib:

import pandas

column_names = ['cat1', 'cat2', 'cat3']
data1 = [100,120,140]
data2 = [150,120,190]
data3 = [100,150,130]

df = pandas.DataFrame([data1, data2, data3], columns = column_names)
df.plot.bar(stacked=True)

stacked bar graph from pandas

nanselm2
  • 1,397
  • 10
  • 11
0

I am assuming you get an error when you try to plot?

Currently, you are using lists and so:

>>> data1 = [1, 2, 3]
>>> data2 = [4, 5, 6]

>>> data+data2
>>> [1, 2, 3, 4, 5, 6]

You can use for example numpy arrays, which should solve your problem.

>>> data1 = numpy.array([1, 2, 3])
>>> data2 = numpy.array([4, 5, 6])

>>> data1+data2
>>> [5, 7, 9]
jonnybazookatone
  • 2,188
  • 15
  • 21
0

How about this? It appeared to have worked for me.

from __future__ import division
from matplotlib import pyplot as plt

data1 = [100,120,140]
data2 = [150,120,190]
data3 = [130,110,120]

f, (ax1, ax2) = plt.subplots(1, 2, figsize=(10,5))

## Absolute count

b = list(r_[data1] +r_[ data2])

ax1.bar(range(len(data1)), data1, label='data 1', alpha=0.5, color='b')
ax1.bar(range(len(data2)), data2, bottom=data1, label='data 2', alpha=0.5, color='r')
ax1.bar(range(len(data3)), data3, bottom=b, label='data 3', alpha=0.5, color='g')
plt.sca(ax1)
plt.xticks([0.4, 1.4, 2.4],  ['category 1', 'category 2', 'category 3'])
ax1.set_ylabel("Count")
ax1.set_xlabel("")
plt.legend(loc='upper left')

## Percent

totals = [i + j + k for i,j,k in zip(data1, data2, data3)]
data1_rel = [i / j * 100 for  i,j in zip(data1, totals)]
data2_rel = [i / j * 100 for  i,j in zip(data2, totals)]
data3_rel = [i / j * 100 for  i,j in zip(data3, totals)]

b_rel = list(r_[data1_rel] +r_[ data2_rel] )


ax2.bar(range(len(data1_rel)), data1_rel, alpha=0.5, color='b')
ax2.bar(range(len(data2_rel)), data2_rel, bottom=data1_rel, alpha=0.5, color='r')
ax2.bar(range(len(data3_rel)), data3_rel, bottom=b_rel, alpha=0.5, color='g')
plt.sca(ax2)
plt.xticks([0.4, 1.4, 2.4],  ['category 1', 'category 2', 'category 3'])
ax2.set_ylabel("Percentage") 
ax2.set_xlabel("")

plt.show()
brechmos
  • 1,278
  • 1
  • 11
  • 22