0

I have several CSV files in this format, named n-data.csv where n is the number of threads:

elapsed,label
120,Step 01
260,Step 02
113,Step 03
100,Step 01
200,Step 02
103,Step 03

and I would like to get the average of each step per thread (or n), and plot them as grouped bar charts much like this: http://matplotlib.sourceforge.net/examples/api/barchart_demo.html

My code works fine with one file (thanks to Yet Another list csv file in Python Question):

#!/usr/bin/env python

import csv
import sys

import pylab as p

fig = p.figure()
ax = fig.add_subplot(1,1,1)

result = dict()
av = []
idx = []
for file in sys.argv[1:]:
  for row in csv.DictReader(open(file)):
    label = row['label']
    elapsed = row['elapsed']
    if label in result:
      result[label].append(elapsed)
    else:
      result[label] = [elapsed]    
  for i in sorted (result.iterkeys()):
    s = sum(int(v) for v in result[i])
    a = s/float(len(result[i]))
    av.append(a)
    idx.append(i)

  y = av
  N = len(y)
  ind = range(N)
  ax.bar(ind, y, facecolor='#56A5EC',
          align='center',label='1 Thread') 
  ax.set_ylabel('Average Response Time')
  ax.set_title('Counts, by group',fontstyle='italic')
  ax.legend()
  ax.set_xticks(ind)
  ax.grid(color='#000000', linestyle=':', linewidth=1)
  group_labels = idx
  ax.set_xticklabels(group_labels)
  fig.autofmt_xdate()  
  p.grid(True) 
  p.show()

However, when I run this script with create_bar.py *csv, all the data gets pushed into av (average) and idx (index), and it aggreates all the data into av and idx -- which works as it should.

How do I break out av and idx so I can create group charts? I tried:

    threads = file.split('-')[0]
    for count in threads
      for row in csv.DictReader(open(file)):
      ...
        av['threads'].append(a)
        idx['threads'].append(i)

to no avail.

1 Answer 1

1

Perhaps have a list of lists ...

av = []
idx = []

for file in sys.argv[1:]:
    next_av = []
    next_idx = []

    ...

    for i in sorted (result.iterkeys()):
        s = sum(int(v) for v in result[i])
        a = s/float(len(result[i]))
        next_av.append(a)
        next_idx.append(i)
    av.append(next_av)
    idx.append(next_idx)

for index in range(len(av)):
    # do something with the lists av[index] and idx[index]
Sign up to request clarification or add additional context in comments.

1 Comment

thanks @jcfollower, after some twisting and turning, i got this to work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.