I have collected some data in a textfile and want to create a boxplot. But this datafile contains rows of variable length, for example.
1.2, 2.3, 3.0, 4.5
1.1, 2.2, 2.9
for equal length I could just do
PW = numpy.loadtxt("./learning.dat")
How do I handle variable lenght data lines?
Just use a list of arrays or lists.
boxplot will take any sort of sequence (Well, anything that has a
__len__, anyway. It won't work with generators, etc.).
import matplotlib.pyplot as plt x = [[1.2, 2.3, 3.0, 4.5], [1.1, 2.2, 2.9]] plt.boxplot(x) plt.show()
If you're asking how to read in your data, there are plenty of ways to do what you want. As a simple example:
import matplotlib.pyplot as plt import numpy as np def arrays_from_file(filename): """Builds a list of variable length arrays from a comma-delimited text file""" output =  with open(filename, 'r') as infile: for line in infile: line = np.array(line.strip().split(','), dtype=np.float) output.append(line) return output plt.boxplot(arrays_from_file('test.txt')) plt.show()