title: plotting without display: correct use of matplotlib.use()
body: <p>I need to plot on a system that doesn't have a display.
  If I plot naively using matplotlib.pyplot.plt I get</p>
  
  <pre><code>    raise RuntimeError('Invalid DISPLAY variable')
  </code></pre>
  
  <p>I have found out that <code>matplotlib.use()</code> can be used in this context.
  Now I have a main file:</p>
  
  <pre><code> **main.py:**
  
   import my_module
   ....
   # do stuff
  
   **my_module.py:**
  
   import matplotlib
   matplotlib.use('Agg') # Must be before importing matplotlib.pyplot or     pylab!
   import matplotlib.pyplot as plt    
   # do stuff
   plt.plot(data)
  </code></pre>
  
  <p>When running main.py I get:</p>
  
  <pre><code>UserWarning: 
  This call to matplotlib.use() has no effect because the backend has already
  been chosen; matplotlib.use() must be called *before* pylab,     matplotlib.pyplot,
  or matplotlib.backends is imported for the first time.
  </code></pre>
  
  <p>But to me it seems I am calling it before importing <code>pyplot</code>. Later I get this runtime error</p>
  
  <pre><code>File "[OMIT]/python2.7/site-packages/matplotlib    /backends/backend_qt5.py", line 144, in _create_qApp
  raise RuntimeError('Invalid DISPLAY variable')
  RuntimeError: Invalid DISPLAY variable
  </code></pre>
  
  <p>So what is the right way to change <code>matplotlib</code> backend?I do not need a display, but I need to be able to store the figures using plt.savefig</p>