python - TypeError when plotting histogram with Matplotlib -


i'm trying plot histogram of file of float numbers. contents of file this:

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 ... 

for reason, attempt throwing me typeerror: len() of unsized object.

import matplotlib.pyplot plt  input_file = "inputfile.csv" file = open(input_file, "r") all_lines = list(file.readlines()) file.close()  line in all_lines:     line = float(line.strip()) # removing '\n' @ end , converting float     if not isinstance(line, float): # verifying data points converted float         print type(line)  print len(all_lines) # 146445  print type(all_lines) # <type 'list'>  plt.hist(all_lines, bins = 10) # line throws error plt.show() 

i have scoured looking similar problems. appears error common when trying plot non-numeric data types, not case here, since explicitly check data type of each number ensure not strange data type.

is there obvious missing?

you loop not convert items of all_lines floats in place; takes each item, converts float , prints it, not change value in list. so, when come plot all_lines, lines still stored strings.

you instead change values in list floats using list comprehension follows:

all_lines = [float(line) line in all_lines] 

even better might read file using numpy, , have lines stored floats in numpy array, , save trouble of iterating through lines of file:

import numpy np import matplotlib.pyplot plt  input_file = "inputfile.csv" all_lines = np.genfromtxt(input_file)  plt.hist(all_lines, bins = 10) plt.show() 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -