Reputation: 653
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()
Upvotes: 8
Views: 15717
Reputation: 3706
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()
Upvotes: 17