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?
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.
A box plot is another way to visualize discrete variables. Instead of showing each number, it shows the range of values in groups.
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.
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()
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With