title: How to change backends in matplotlib / Python
body: <p>I am struggling with the following issue. I need to generate reports that consists of a collection of charts. All these charts, except one, are made using Matplotlib default backend (TkAgg). One chart needs to be made using the Cairo backend, the reason is that I am plotting an igraph graph and that can only be plotted using Cairo. </p>
  
  <p>The issue is that I cannot change backends on the fly, for example the following does not work:<br>
      <code>matplotlib.pyplot.switch_backend('cairo.png')</code>
  (I know that the switch_backend functionality is experimental)</p>
  
  <p>and I have also tried <code>matplotlib.use("cairo.png")</code> but this leads to import problems as the <code>matplotlib.use("cairo.png")</code> statement should come before importing <code>matplotlib.pyplot</code>. 
  but I need two different backends over the course of the life of the script. </p>
  
  <p>So my question is does someone have a code snippet that shows how to switch the backend in Matplotlib?</p>
  
  <p>Thanks so much!</p>
  
  <p>UPDATE:
  I have written a snippet that loads matplotlib, shows the default backend, unloads matplotlib, reloads it and changes the backend:</p>
  
  <pre><code>import matplotlib
  import matplotlib.pyplot as plt
  import sys
  print matplotlib.pyplot.get_backend()
  
  modules = []
  for module in sys.modules:
      if module.startswith('matplotlib'):
          modules.append(module)
  
  for module in modules:
      sys.modules.pop(module)
  
  import matplotlib
  matplotlib.use("cairo.png")
  import matplotlib.pyplot as plt
  
  print matplotlib.pyplot.get_backend()
  </code></pre>
  
  <p>but is this really the way to do it? </p>
  
  <p>UPDATE 2: I had some serious brain freeze yesterday... The simple and most obvious solution is to use the Cairo backend for all charts and not to switch the backend at all :)</p>
  
  <p>UPDATE 3: Actually, it's still an issue so anybody who knows how to dynamically switch matplotlib backends....please post your answer. </p>