Link to home
Start Free TrialLog in
Avatar of SiHodgy007
SiHodgy007Flag for United Kingdom of Great Britain and Northern Ireland

asked on

How do you stack a bar chart in Matlibplot using a loop

Stacking a bar chart in matplotlib using a loop, I can do it for one set of data but struggling to loop the bottom parameter to stack the data.

Data



N =9
ind = np.arrange(N)

stacker = 0
for idx, ev in enumerate(sample):
      
    r = [int(t.seconds/3600 for t in ev.duration]
    if idx ==0:
        plt.bar(ind, r)
        stacker =r
    else:
         plt.bar(ind, r, bottom=stacker)
         stacker = r

plt.show


   
      

Open in new window

Avatar of noci
noci

Here is a description: https://matplotlib.org/3.1.3/gallery/lines_bars_and_markers/bar_stacked.html#sphx-glr-gallery-lines-bars-and-markers-bar-stacked-py


You only present half of the code (imports statements are missing)
Also valid sample data is missing... Can you provide these?
Avatar of SiHodgy007

ASKER

import numpy as np
import sys
import matplotlib.pyplot as plt
import pandas as pd
from datetime import timedelta as td


sample = ['Test1', 'Test2']
# the value is timedelta dataframe
evDict = {'Test1':[Timedelta('0 days 05:08:28'),Timedelta('0 days 03:08:28')],
           'Test2':[Timedelta('0 days 05:08:28'),Timedelta('0 days 03:08:28')]}


N =2
ind = np.arange(N)

stacker = 0
for idx, ev in enumerate(sample):
      
    r = [int(t.seconds/3600) for t in evDict[ev]]
    if idx ==0:
        plt.bar(ind, r)
        stacker = r
    else:
         plt.bar(ind, r, bottom=stacker)
         stacker = r

plt.show

Open in new window

What are the modules for np & plt. The imports are still missing, TImedelta is an unknown function from what library does that get imported.
the datetime module has a timedelta that does not accept strings as parameter.
the standard numpy has no arrange() function.
Updated above
Please try again at a working reproducer.

run python yourscript from a command line: until it shows no more errors please.

Hints:
dataframe is an unknown module,
np.arrange() probably needs to be np.arange()
there is an ) missing in the expression r = .....    
evDict has no names... how to refer to evDict[ev].duration
Updated


import numpy as np
import sys
import matplotlib.pyplot as plt
import pandas as pd
from datetime import timedelta as td

class Sample:
    def __init__(self, name, tasks, duration):
        self.name = name
        self.tasks = tasks
        self.duration = duration

df = pd.read_excel('sample.xlsx')
print(df)

sampleDict = {}
for ev in df.Sample.unique():
    dur = []
    for tsk in df.Tasks.unique():
        dur.append(df.query('Tasks == "%s" and Sample == "%s"' %(tsk, ev))['End Date'].max() - 
                   df.query('Tasks == "%s" and Sample == "%s"' %(tsk, ev))['Start Date'].min())
    sampleDict[ev] = Sample(ev, df.Tasks.unique().tolist(), dur)
N = 2
ind = np.arange(N)

stacker = 0
for idx, ev in enumerate(df.Sample.unique()):
    r = [int(t.seconds/60) for t in sampleDict[ev].duration]
    if idx ==0:
        plt.bar(ind, r)
        stacker = r
    else:
        plt.bar(ind, r, bottom=stacker)
        stacker = r
plt.xticks(ind, sampleDict[ev].tasks)
plt.legend(df.Sample.unique())
plt.show()

Open in new window

sample.xlsx
ASKER CERTIFIED SOLUTION
Avatar of noci
noci

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent,