Boxplot dictionaries instead of lists?
Question: Let us say I want to create a boxplot of a list which contains the numbers 1-5
about a million times each.

Such a list would be of about size 5 000 000, however represented as a dict it
takes no space at all:

    
    
    s = {1: 1000000, 2: 1000000, 3: 1000000, 4: 1000000, 5:1000000}
    

The problem is, if I try to create a boxplot of that dict I get the error

    
    
    Traceback (most recent call last):
      File "<pyshell#17>", line 1, in <module>
        ax.boxplot(s)
      File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/matplotlib/axes.py", line 5462, in boxplot
        if not hasattr(x[0], '__len__'):
    KeyError: 0
    

Is there a clever way of boxplotting the dictionary `s`, without having to put
all the elements in a list?

* * *

A comment suggested I try

    
    
    boxplot(n for n, count in s.iteritems() for _ in xrange(count))
    

but this resulted in

    
    
    Traceback (most recent call last):
      File "<pyshell#7>", line 1, in <module>
        boxplot(n for n, count in s.iteritems() for _ in xrange(count))
      File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/matplotlib/pyplot.py", line 2134, in boxplot
        ret = ax.boxplot(x, notch, sym, vert, whis, positions, widths, patch_artist, bootstrap)
      File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/matplotlib/axes.py", line 5462, in boxplot
        if not hasattr(x[0], '__len__'):
    TypeError: 'generator' object has no attribute '__getitem__'
    


Answer: The whole point of using pictures to describe data is to get a feeling for the
data as a whole, not to be terribly exact. So there will not be much harm in
condensing your data by generating one representative data point for every
1000 actual data points:

    
    
    x = [val for val, num in s.items() for i in range(num//1000)]
    

This should be good enough for the naked eye:

    
    
    import matplotlib.pyplot as plt
    import numpy as np
    s = {1: 1000000, 2: 1000000, 3: 1000000, 4: 1000000, 5:1000000}
    x = [val for val, num in s.items() for i in range(num//1000)]
    dct = plt.boxplot(x)
    plt.show()
    

