title: passing a plot from matpotlib to a flask view
body: <p>I am trying to plot some values and pass them to a Flask template. I tried using this method: <a href="https://stackoverflow.com/questions/34492197/how-to-render-and-return-plot-to-view-in-flask">How to render and return plot to view in flask?</a> </p>
  
  <p>Unfortunately I get this error.</p>
  
  <blockquote>
    <p>TypeError: string argument expected, got 'bytes'</p>
  </blockquote>
  
  <p>I got through to the template when changed the img type to BytesIO, but still it wouldn't show the plot.</p>
  
  <p>I am dead in the water here, can anybody help?</p>
  
  <p>Here is my plot.py</p>
  
  <pre><code>@app.route('/plot', methods=['POST']) # I use POST because I will introduce user specified data
  def plot():
  if request.method == 'POST':
  
      img = io.BytesIO()
      x = [1,2,3,6,7]
      y = [4,6,7,9,10]
      plt.plot(x, y)
      plt.savefig(img, format='png')
      plt.savefig("../xxxx.png") # this works just fine
      img.seek(0)
      plot_url = base64.b64encode(img.getvalue())
      return render_template('plot.html', plot_url=plot_url)
  </code></pre>
  
  <p>And this is plot.html:</p>
  
  <pre><code>{% extends "layout.html" %}
  {% block body %}
  
  
  &lt;img src="data:image/png;base64, {{ plot_url }}" width="20" height="20" alt="graph"&gt;
  {% endblock %}
  </code></pre>