【发布时间】:2018-02-22 18:26:44
【问题描述】:
我正在尝试绘制浮点数文件的直方图。文件内容如下所示:
0.1066770707640915
0.0355590235880305
0.0711180471760610
0.4267082830563660
0.0355590235880305
0.1066770707640915
0.0698755355867468
0.0355590235880305
0.0355590235880305
0.0355590235880305
0.0355590235880305
0.0355590235880305
0.2844721887042440
0.0711180471760610
0.0711180471760610
0.0355590235880305
0.0355590235880305
0.1422360943521220
0.0355590235880305
0.0355590235880305
0.0711180471760610
0.0355590235880305
0.0355590235880305
0.0355590235880305
...
出于某种原因,我的尝试是给我一个TypeError: len() of unsized object。
import matplotlib.pyplot as plt
input_file = "inputfile.csv"
file = open(input_file, "r")
all_lines = list(file.readlines())
file.close()
for line in all_lines:
line = float(line.strip()) # Removing the '\n' at the end and converting to float
if not isinstance(line, float): # Verifying that all data points could be converted to float
print type(line)
print len(all_lines)
# 146445
print type(all_lines)
# <type 'list'>
plt.hist(all_lines, bins = 10) # This line throws the error
plt.show()
我已经搜索过类似的问题。尝试绘制非数字数据类型时,此错误似乎很常见,但此处并非如此,因为我明确检查了每个数字的数据类型以确保它们不是奇怪的数据类型。
我有什么明显的遗漏吗?
【问题讨论】:
标签: python python-2.7 matplotlib histogram