Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boxplot from dictionary with different length

Tags:

python

I have one dictionary with different length of the values for each key. Trying to plot as a boxplot, I cant go forward. is there any other method to do this?

I wrote this but it doesn't work:

import matplotlib.pyplot as plt 
import matplotlib as mpl 

dict1 = {'Pb': [53.0, 56.0, 56.0, 57.0, 57.0, 57.0, 46.0], 'Pa': [56.0, 55.0], 'Pg': [57.0, 57.0, 58.0, 57.0, 57.0, 57.0, 57.0, 57.0,53.0, 57.0, 55.0, 58.0, 58.0, 58.0, 57.0, 57.0, 57.0, 57.0, 57.0, 55.0, 55.0, 57.0, 57.0, 55.0, 58.0, 58.0, 58.0, 55.0, 58.0, 54.0, 58.0, 57.0, 57.0, 58.0, 55.0, 56.0, 55.0, 55.0, 55.0, 58.0, 56.0, 57.0, 57.0, 57.0, 57.0, 56.0, 57.0, 56.0],'Pf': [54.0], 'Pn': [56.0, 56.0, 55.0, 56.0, 56.0, 56.0], 'Ps': [58.0, 56.0, 57.0, 56.0, 56.0, 55.0, 56.0, 56.0, 56.0, 55.0, 56.0, 56.0, 56.0, 58.0, 57.0, 58.0, 57.0, 57.0, 56.0, 58.0, 56.0, 53.0, 56.0, 56.0, 56.0, 56.0, 56.0, 56.0]}

      # remove from here
for k, v in dict1.iteritems():
      boxplot(k,v) 
      plt.show()

The solution proposed and tested: Just substitute the interpretation to the text below. It works with python 3.5

#And include this:
labels, data = [*zip(*dict1.items())]  # 'transpose' items to parallel key, value lists
    plt.boxplot(data)
    plt.xticks(range(1, len(labels) + 1), labels)
    plt.show()
like image 657
F.Lira Avatar asked Dec 05 '17 15:12

F.Lira


People also ask

How do you change the size of a box plot 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.

How do you customize a boxplot?

Customizing Box PlotThe notch = True attribute creates the notch format to the box plot, patch_artist = True fills the boxplot with colors, we can set different colors to different boxes. The vert = 0 attribute creates horizontal box plot. labels takes same dimensions as the number data sets.

Can we customize the whiskers in a boxplot?

However, boxplot() can only set cap values of whiskers as the values of percentiles. e.g. Given my distribution is not a normal distribution, then the 95th/5th percentiles will not be the (mean+2std)/(mean-2std).


1 Answers

did you want all the values together?
[n for v in dict1.values() for n in v] 'flattens' the list of values

then a boxplot is a matplotlib standard plot https://matplotlib.org/examples/pylab_examples/boxplot_demo.html

from matplotlib import pyplot as plt


plt.boxplot([n for v in dict1.values() for n in v])

enter image description here

or by key

# Python 3.5+
labels, data = [*zip(*dict1.items())]  # 'transpose' items to parallel key, value lists

# or backwards compatable    
labels, data = dict1.keys(), dict1.values()

plt.boxplot(data)
plt.xticks(range(1, len(labels) + 1), labels)
plt.show()

enter image description here

like image 68
f5r5e5d Avatar answered Sep 23 '22 22:09

f5r5e5d