Python列表中的Python列表,用于创建分组条形图

时间:2011-08-03 19:58:42

标签: python list csv charts matplotlib

我有几种这种格式的CSV文件,名为n-data.csv,其中n是线程数:

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

我希望获得每个线程(或n)每个步骤的平均值,并将它们绘制为分组条形图,如下所示:http://matplotlib.sourceforge.net/examples/api/barchart_demo.html

我的代码适用于一个文件(感谢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()

但是,当我使用create_bar.py *csv运行此脚本时,所有数据都会被推送到av(平均值)和idx(索引),并且它会将所有数据聚合到av和idx - 它应该工作。

如何突破avidx以便创建群组图表?我试过了:

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

无济于事。

1 个答案:

答案 0 :(得分:1)

也许有一份清单......

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]