【发布时间】:2019-11-19 22:54:09
【问题描述】:
我正在为一门课程研究这个实验室,但我不断收到标题中的错误。我们应该做的是从 csv 文件制作条形图,我真的不知道为什么会出现这些错误,但它看起来像是由于这条线
college_names, college_enrollments = read_file(file_name)
我不应该改变。 这是整个代码(我也在 plt.whatever 行中添加了):
import numpy as np
import matplotlib.pyplot as plt
def read_file(file_name):
file = open(file_name, 'r')
dlist = []
a = []
b = []
with open(file_name, 'r') as f:
next(f)
for line in f:
line = line.replace('\n', '')
x = line.split(",")
a.append(x[0])
b.append(x[1])
f.close
college_names = np.array(a)
college_enrollments = np.array(b)
# -------------------------------------------------
def main(file_name):
college_names, college_enrollments = read_file(file_name)
plt.bar(college_names, college_enrollments)
plt.xlabel('Department')
plt.ylabel('# of students')
plt.title('amount of students by department')
plt.show()
# -------------------------------------------------
main("fall-2019.csv")
追溯:
Traceback(最近一次调用最后一次): 文件“C:\Users\letho\OneDrive\Desktop\csci\hunchback2.py”,第 53 行,在 主要(“2019年秋季.csv”) 文件“C:\Users\letho\OneDrive\Desktop\csci\hunchback2.py”,第 38 行,在 main 大学名称,大学注册 = 读取文件(文件名) TypeError: 无法解压不可迭代的 NoneType 对象
【问题讨论】:
-
你能添加完整的堆栈跟踪吗?
-
@DarrenChristopher 我添加了我认为您要求的内容
-
您忘记从函数中返回数组。
-
@ImportanceOfBeingErnest 你说的完全正确,我不敢相信我在为这么简单的事情绞尽脑汁,谢谢。
标签: python numpy matplotlib