title: Multiple route mapping to different matplotlib graphs in flask app
body: <p>I have this "flask app" with two links, each mapping to different matplotlib visualizations, for example: localhost:5000/line_chart and localhost:5000/bar_chart. </p>
  
  <p>When I start the server, and click the a route (any of them), I see what I expect.</p>
  
  <p>localhost:5000/bar_chart</p>
  
  <p><a href="https://i.stack.imgur.com/SbJQw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SbJQw.png" alt="enter image description here"></a></p>
  
  <p>When I go back and view the other link, both graphs break. </p>
  
  <p>localhost:5000/line_chart</p>
  
  <p><a href="https://i.stack.imgur.com/zHPx7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zHPx7.png" alt="enter image description here"></a></p>
  
  <p>localhost:5000/bar_chart</p>
  
  <p><a href="https://i.stack.imgur.com/E7q3f.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E7q3f.png" alt="enter image description here"></a></p>
  
  <p>I can reproduce this every time by closing the server then running the "run.py" script again. Seems to be an overwriting conflict with the in-memory buffer. Has anyone had this issue before?</p>
  
  <p>app/views.py</p>
  
  <pre><code>import matplotlib
  matplotlib.use('Agg') # this allows PNG plotting
  import matplotlib.pyplot as plt
  import base64
  
  from flask import render_template
  from app import app
  from io import BytesIO
  
  @app.route('/')
  @app.route('/index')
  def index():
  res = ''
  navigation = [['Line Chart','line_chart'],['Bar Chart','bar_chart']]
  res = res + '&lt;h1&gt;Matplotlib Chart Examples&lt;/h1&gt;'
  res = res + '&lt;ul&gt;'
  
  for item in navigation:
      name = item[0]
      link = item[1]
      res = res + '&lt;li&gt;&lt;a href="' + link + '"&gt;'+ name +'&lt;/a&gt;&lt;/li&gt;'
  
  res = res +'&lt;/ul&gt;'
  return res
  
  
  @app.route('/bar_chart')
      def bar_chart():
  
      movies = ["Annie Hall", "Ben-Hur", "Casablanca", "Gandhi", "West Side Story"]
      num_oscars = [5, 11, 3, 8, 10]
  
      # bars are by default width 0.8, so we'll add 0.1 to the left coordinates
      # so that each bar is centered
  
      xs = [i + 0.1 for i, _ in enumerate(movies)]
  
      # plot bars with left x-coordinates [xs], heights [num_oscars]
      plt.bar(xs, num_oscars)
      plt.ylabel("# of Academy Awards")
      plt.title("My Favorite Movies")
  
      # label x-axis with movie names at bar centers
      plt.xticks([i + 0.5 for i, _ in enumerate(movies)], movies)
  
      return compute(plt)
  
  
  @app.route('/line_chart')
  def line_chart():
  
      years = [1950, 1960, 1970, 1980, 1990, 2000, 2010]
      gdp = [300.2, 543.3, 1075.9, 2862.5, 5979.6, 10289.7, 14958.3]
  
      # create a line chart, years on x-axis, gdp on y-axis
      plt.plot(years, gdp, color='green', marker='o', linestyle='solid')
  
      # add a title
      plt.title("Nominal GDP")
  
      # add a label to the y-axis
      plt.ylabel("Billions of $")
  
      return compute(plt)
  
  
  def compute(plt):
      # run plt.plot, plt.title, etc.
      figfile = BytesIO()
      plt.savefig(figfile, format='png')
      figfile.seek(0)  # rewind to beginning of file
  
      #figfile.getvalue()  extracts string (stream of bytes)
      figdata_png = base64.b64encode(figfile.getvalue())
  
      return render_template('index.html',
                         title='matplotlib chart',
                         results=figdata_png)
  </code></pre>
  
  <p>Thank you for your time. </p>