在python中绘制条形图时出错

时间:2017-12-06 23:43:09

标签: python csv matplotlib graph

我目前的代码如下。代码应该工作的方式是询问用户名,然后在被调用的csv中打印与该用户关联的所有项目和小时,然后绘制一个垂直条形图,其中x值是项目名称和y值是该项目的时间。我遇到了一个错误,如下所示。如何从代码中提取正确的值并相应地绘制它们?我是python的新手,所以任何你可以借出的帮助都会很棒。 提前致谢

import csv
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt

with open('Report1.csv') as csvfile:
  hour_summation = {}
  read_csv = csv.reader(csvfile, delimiter=',')
  name_found = False
  while not name_found:
    # take user input for name
    full_name = input('Enter your full name: ').lower()
    # start search at top of file. Have to do this or each loop will
    # start at the end of the file after the first check.
    csvfile.seek(0)
    for row in read_csv:
      # check to see if row matches name
      if(' '.join((row[0], row[1]))).lower() == full_name.strip().lower():
        name_found = True
        hour_summation[row[2]] = hour_summation.get(row[2], 0) + int(float(row[3]))

    # name wasn't found, so repeat process
    if not name_found:
      # name wasn't found, go back to start of while loop
      print('Name is not in system')
    else:
      # break out of while loop. Technically not required since while
      # will exit here anyway, but clarity is nice
      break

print('This is {} full hours report:'.format(full_name))
for k, v in hour_summation.items():
    print(k + ': ' + str(v) + ' hours')

for k, v in hour_summation.items():
    x = np.array(k)
    y = np.array(v)
    y_pos = np.arange(len(y))
    plt.bar(y_pos, y, align='center', alpha=0.5)
    plt.xticks(y_pos, x)
    plt.ylabel('hours')
    plt.title('Projects for: ', full_name)
    plt.show()

这是我得到的错误。它正在做所有事情,直到它到达图形部分。

Enter your full name: david ayers
This is david ayers full hours report:
AD-0001  Administrative Time: 4 hours
AD-0002  Training: 24 hours
AD-0003  Personal Time: 8 hours
AD-0004  Sick Time: 0 hours
OPS-0001  General Operational Support: 61 hours
SUP-2507  NAFTA MFTS OS Support: 10 hours
ENG-2001_O Parts Maturity-Overhead: 1 hours
ENG-2006_O CHEC 2 Tool-Overhead: 19 hours
FIN-2005  BLU Lake BOM Analytics: 52 hours




---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-c9c8a4aa3176> in <module>()
     36     x = np.array(k)
     37     y = np.array(v)
---> 38     y_pos = np.arange(len(y))
     39     plt.bar(y_pos, y, align='center', alpha=0.5)
     40     plt.xticks(y_pos, x)

TypeError: len() of unsized object

0 个答案:

没有答案