I'm trying to plot four stacked bar charts on the same plot using Python's matplotlib library.
For each observation (obs1, obs2, obs3, obs4), I want to view the quantity of each component (c1, c2, c3, c4, c5, c6) using a stacked bar chart. This is the code I have written:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = np.array([[-904., 97., 59., 5., 252., 138.], [-603., 65., 0., 29., 0., 0.], [-571., -27., 0., -28., 0., 0.], [-80., 40., 0., -9., 0., 0.]])
data2 = pd.DataFrame(data=data)
data2.index = ['obs1', 'obs2', 'obs3', 'obs4']
data2.columns = ['c1', 'c2', 'c3', 'c4', 'c5', 'c6']
ind = np.arange(4)
width = 0.4
p1 = plt.bar(ind, data2['c1'], width)
p2 = plt.bar(ind, data2['c2'], width)
p3 = plt.bar(ind, data2['c3'], width)
p4 = plt.bar(ind, data2['c4'], width)
p5 = plt.bar(ind, data2['c5'], width)
p6 = plt.bar(ind, data2['c6'], width)
plt.legend((p1[0], p2[0], p3[0], p4[0], p5[0], p6[0]), tuple(data2.columns), bbox_to_anchor = (1.05, 1), loc = 'upper left', borderaxespad = 0.)
For clarity, this is the DataFrame (used to produce the plot):
print(data2)
c1 c2 c3 c4 c5 c6
obs1 -904.0 97.0 59.0 5.0 252.0 138.0
obs2 -603.0 65.0 0.0 29.0 0.0 0.0
obs3 -571.0 -27.0 0.0 -28.0 0.0 0.0
obs4 -80.0 40.0 0.0 -9.0 0.0 0.0
This is the plot:
Note on the plot, the bar for obs1 is at x=0, the bar for obs2 is at x=1, and so on.
However, there are two problems:
obs1 has a value of 252 for component 5, but the height of component 5 (in purple) is substantially below 252. How can I fix this?
obs3 has a value of -27 for component 2, but this is not shown on the plot at all. How can I solve this?
Thanks.


