Boxplot with variable length data in matplotlib


Question

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")
matplotlib.boxplot(PW.T);

How do I handle variable lenght data lines?

1
10
1/30/2011 12:10:24 PM

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

E.g.:

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()

enter image description here

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()
19
1/30/2011 3:30:00 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon