title: Dynamically generate bar plot graph with Python 3 and matplotlib
body: <p>I have this Python 3 script that uses the 'collections' module to simply print out common words in a block of text and the number of times those words appear in that text.</p>
  
  <pre><code>    word_counter = collections.Counter(text)
      for word, count in word_counter.most_common(10):
         print(word, ": ", count)
  </code></pre>
  
  <p>For example, it may print this out:   </p>
  
  <ul>
  <li>red : 5 </li>
  <li>sun : 2</li>
  <li>planet: 10</li>
  <li>moon : 7</li>
  <li>hydrogen : 22</li>
  </ul>
  
  <p>I have another script that uses the matplotlib library and generates a bar plot graph:</p>
  
  <pre><code>    words = ('red', 'Sun', 'planet', 'moon', 'hydrogen')
      y_pos = np.arange(len(words))
      wordCount = [5,2,10,7,22]
      plt.bar(y_pos, wordCount, align='center', alpha=0.5)
      plt.xticks(y_pos, words)
      plt.ylabel('Count')
      plt.title('Common Word Count')
      plt.savefig('wordcount.png')
      plotImage = "wordcount.png"
      htmldata = """
      &lt;div&gt;
          &lt;img src="{plotImage}" /&gt;
      &lt;/div&gt;""".format(plotImage = plotImage)
      print(htmldata)  
  </code></pre>
  
  <p>So you can see I put static data in the that script for the words and the wordCount.</p>
  
  <p>Is there a way to combine the two scripts I have so that they work together?  So the "words" and "wordCount" variable would be populated with data from the 'for word, count...' loop?</p>
  
  <p>That way I could feed it whatever text or paragraph I wanted and not have to hard-code any values.</p>
  
  <p>I tried to add these two lines under the 'for word, count ... ' loop:</p>
  
  <pre><code>myWords = myWords + word + ","
  myCount += [count]
  </code></pre>
  
  <p>But that throws this error:</p>
  
  <blockquote>
    <p>ValueError: shape mismatch: objects cannot be broadcast to a single
    shape</p>
  </blockquote>
  
  <p>Thanks!</p>