title: Matplotlib histogram where each bin is colored by frequency of additional parameter
body: <p>If I have </p>
  
  <pre><code>data = [(1, 'a'), (1, 'b'), (1, 'a'), (2, 'a'), (2, 'b'), (3, 'c'), (3, 'c'), (3, 'c'), (3, 'c')] 
  </code></pre>
  
  <p>such that there are two properties for each datapoint:</p>
  
  <pre><code>x, y = zip(*data)
  </code></pre>
  
  <p>I can display <code>x</code> in a histogram, ala: </p>
  
  <pre><code>x = [1, 1, 1, 2, 2, 3, 3, 3, 3]
  bins = [1, 2, 3]; f = [3, 2, 4]`. 
  </code></pre>
  
  <p>Then, using the second property, </p>
  
  <pre><code>y = ['a', 'b', 'a', 'a', 'b', 'c', 'c', 'c', 'c']
  </code></pre>
  
  <p>each bin from the original histogram has frequency information for the secondary parameter: </p>
  
  <pre><code>bins[0] = {'a': 2, 'b': 1}
  bins[1] = {'a': 1, 'b': 1}
  bins[2] = {'b': 1, 'c': 3}
  </code></pre>
  
  <p>Using <code>matplotlib</code>, I can create the basic histogram of <code>x</code>:</p>
  
  <pre><code>import matplotlib.pyplot as plt
  
  fig = plt.figure()
  ax = fig.add_subplot(111)
  n, bins, patches = ax.hist(x, 3)
  </code></pre>
  
  <p>Is there a clever way to iterate over the patches, perhaps, to break them up into appropriately sized rectangles that reflect the additional information, <code>y</code>?</p>
  
  <p>In the example, if I wanted <code>'a'</code> to be <em>red</em>, <code>'b'</code> to be <strong>green</strong> and <code>'c'</code> to be <strong><em>blue</em></strong>, then first bin (x = 1) would be <em>two-thirds red</em> and <strong>one-third green</strong>, the second bin (x = 2) would be <em>half red</em> and <strong>half green</strong> and the final bin (x = 3) would be <strong>one-fourth green</strong> and <strong><em>three-fourths blue</em></strong>.</p>
  
  <p><a href="http://i.imgur.com/LUiIU0o.png" rel="nofollow">Example illustration</a></p>