title: RuntimeError: main thread is not in main loop using Matplotlib with Django
body: <p>I am creating a Matplotlib figure to display in a HTML template in my Django application. I send this figure to the HTML by saving it under my static files, and then loading an <code>img</code> tag with the saved <code>.png</code>.
  I do this in my <code>views.py</code> after getting a reference to the figure.</p>
  
  <pre><code>        # Get analysis visualization chart
          figure = analyser.visualize_tweets(emotions)
  
          # Save figure in static folder as png
          figure.savefig('static/analysis_figures/figure.png')
  
          # Inject html with figure path
          response['analysis_figure_path'] = 'analysis_figures/figure.png'
  
  return render(request, 'landing_page/index.html', response)
  </code></pre>
  
  <p>My HTML is something like this:</p>
  
  <pre><code>&lt;img src={% static analysis_figure %} alt=""&gt;
  </code></pre>
  
  <p>However, this caused the <code>RuntimeError: main thread is not in main loop</code> to happen when the function in my <code>views.py</code> is called a second time (if it's called once everything works properly).
  To prevent this error, I moved saving the Matplotlib figure to <code>main()</code> as to run in the main thread and then call it in my original function. This fixed the error, but prevents my HTML from reloading so every time the user submits a query the new figure is displayed over the previous one without the previous one being removed.
  Any ideas on any of the problems ?</p>