Stacking multiple columns in a stacked bar plot using matplotlib in python 3 -


i trying generate stacked bar plot keep track of 3 parameters every hour on multiple days. picture of sample plot sampleplot. however, have had no success plotting in python. fact beginner in python, makes matters worse.

two attempts made answer questions are: horizontal stacked bar chart in matplotlib , stack bar plot in matplotlib , add label each section (and suggestions). however, have not been able achieve desired results following of above solutions.

can please provide me guidance how generate plot or point me in direction?

edit 1: code have written follows:

import matplotlib.pyplot plt; plt.rcdefaults() import numpy np import matplotlib.pyplot plt  status_day1 = [[0.2,0.3,0.5], [0.1,0.3,0.6], [0.4,0.4,0.2], [0.6,0.1,0.4]] status_day2 = [[0.1,0.2,0.7], [0.3,0.2,0.5], [0.1,0.5,0.4], [0.2,0.5,0.3]]  day = ('day1', 'day2')  fig = plt.figure(figsize=(10,8)) ax = fig.add_subplot(111) x in range(0,4): #looping through every hour     y in range(0,3): #looping through every parameter         if y==0:             ax.bar(1, status_day1[x][y],color='b',align='center')         elif y==1:             ax.bar(1, status_day1[x][y],color='r',align='center')         else:             ax.bar(1, status_day1[x][y],color='g',align='center')  # assuming 3 parameters every hour getting stacked on top of 1            x in range(0,4):     y in range(0,3):         if y==0:             ax.bar(1, status_day2[x][y],color='b',align='center')         elif y==1:             ax.bar(1, status_day2[x][y],color='r',align='center')         else:             ax.bar(1, status_day2[x][y],color='g',align='center')   ax.set_xticklabels(day)  ax.set_xlabel('day')   ax.set_ylabel('hours')  plt.show() 

the undesired result is: incorrect plot

you need keep track of bottom of bars, see stacked bar plot example in matplotlib docs: http://matplotlib.org/examples/pylab_examples/bar_stacked.html

also, can rid of of more uglier loop code using python's zip , enumerate functions as

for value in data:      print(value) 

instead of

for in range(len(data)):     print(data[i]) 

with expected result:

import matplotlib.pyplot plt  status_day1 = [     [0.2, 0.3, 0.5],     [0.1, 0.3, 0.6],     [0.4, 0.4, 0.2],     [0.6, 0.1, 0.4], ]  status_day2 = [     [0.1, 0.2, 0.7],     [0.3, 0.2, 0.5],     [0.1, 0.5, 0.4],     [0.2, 0.5, 0.3], ]  days = ('day1', 'day2')  fig = plt.figure(figsize=(10, 8)) ax = fig.add_subplot(1, 1, 1)  day, data in enumerate((status_day1, status_day2)):     bottom = 0     hour in data:  # looping through every hour         value, color in zip(hour, ('b', 'r', 'g')):             ax.bar(                 day,                 value,                 bottom=bottom,                 color=color,                 align='center',             )             bottom += value   ax.set_xticks([0, 1]) ax.set_xticklabels(days) ax.set_xlabel('day') ax.set_ylabel('hours') plt.show() 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -