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