Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boxplot with variable length data in matplotlib

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 length data lines?

like image 848
Kabbo Avatar asked Jan 30 '11 12:01

Kabbo


People also ask

How do you change the size of a boxplot in Python?

Steps. Set the figure size and adjust the padding between and around the subplots. Make a Pandas dataframe, i.e., two-dimensional, size-mutable, potentially heterogeneous tabular data. Make a box and whisker plot, using boxplot() method with width tuple to adjust the box in boxplot.

Can you use a box plot for discrete data?

A box plot is another way to visualize discrete variables. Instead of showing each number, it shows the range of values in groups.

How do you make a boxplot for each feature in the dataset Python?

Creating Box PlotThe matplotlib. pyplot module of matplotlib library provides boxplot() function with the help of which we can create box plots. The data values given to the ax. boxplot() method can be a Numpy array or Python list or Tuple of arrays.


1 Answers

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()
like image 182
Joe Kington Avatar answered Oct 07 '22 11:10

Joe Kington