title: Sorting Histogram by results in Python
body: <p>I'm trying to create a histogram in Python, but I want the histogram to have its bars sorted by the results - i.e. I have a data set with approx. 260K lines, and the histogram is counting the number of lines for each state in the USA. I've worked out how to sort the states alphabetically but I can't work out how to have the bars listed in descending order of frequency... my code is below. </p>
  
  <p>Many thanks in advance!</p>
  
  <pre><code>import matplotlib.pyplot as plt
  from collections import OrderedDict
  from collections import Counter
  
  def iter_values(data, column_key):
      for row in data:
          yield(row[column_key])
  
  state = set(iter_values(data, STATE))
  
  def make_bar_chart(data, column_key, values, y_min, y_max): 
      c = Counter(row[column_key] for row in data)
      d = OrderedDict([(k,c[k]) if k in sorted(c) else (k,0) for k in values])
      # bars are by default width 0.8, so we'll add 0.1 to the left coordinates
      xs = [i+0.1 for i,_ in enumerate(values)]
      plt.bar(xs, d.values())
      plt.ylabel('Number of incidents')
      plt.axis([0,len(values),y_min,y_max])
      plt.title(column_key)
      plt.xticks([i + 0.5 for i, _ in enumerate(values)], values, rotation='vertical')
      plt.show()
  
  make_bar_chart(data, STATE, sorted(state), 0, 20000)
  </code></pre>