<h3 dir="auto">Bug report</h3>
<p dir="auto"><strong>Bug summary</strong><br>
<code class="notranslate">matplotlib.pyplot</code> isn't friendly to remotely run commands with no X server:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="In [1]: import matplotlib.pyplot

In [2]: QXcbConnection: Could not connect to display 
Aborted"><pre class="notranslate"><span class="pl-v">In</span> [<span class="pl-c1">1</span>]: <span class="pl-s1">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">pyplot</span>

<span class="pl-v">In</span> [<span class="pl-c1">2</span>]: <span class="pl-v">QXcbConnection</span>: <span class="pl-v">Could</span> <span class="pl-c1">not</span> <span class="pl-s1">connect</span> <span class="pl-s1">to</span> <span class="pl-s1">display</span> 
<span class="pl-v">Aborted</span></pre></div>
<p dir="auto">However, selecting the backend before hand fixes the problem:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="In [1]: import matplotlib

In [2]: matplotlib.use(&quot;Agg&quot;)

In [3]: import matplotlib.pyplot"><pre class="notranslate"><span class="pl-v">In</span> [<span class="pl-c1">1</span>]: <span class="pl-s1">import</span> <span class="pl-s1">matplotlib</span>

<span class="pl-v">In</span> [<span class="pl-c1">2</span>]: <span class="pl-s1">matplotlib</span>.<span class="pl-en">use</span>(<span class="pl-s">"Agg"</span>)

<span class="pl-v">In</span> [<span class="pl-c1">3</span>]: <span class="pl-s1">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">pyplot</span></pre></div>
<p dir="auto">I am not very familiar, but could there be some check of the display module before importing? My problem is I obtain this error when running on a remote server with no X11 forwarding and importing a library that by default imports <code class="notranslate">maplotlib.pyplot</code>. (The library is <a href="http://www.github.com/soft-matter/pims"><code class="notranslate">pims</code></a> )</p>
<p dir="auto"><strong>Matplotlib version</strong></p>
<ul dir="auto">
<li>Operating System: Linux (Debian)</li>
<li>Matplotlib Version: 1.5.3</li>
<li>Python Version: 3.5.3</li>
<li>IPython Version :  5.1.0</li>
</ul>
<p dir="auto">I installed matplotlib via pip.</p>
<p dir="auto">This is reproducible on any headless environment using matplotlib 1.5.1. On my system, the following script:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from matplotlib import pyplot as plt
plt.plot([1, 2], [1, 2])"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">matplotlib</span> <span class="pl-k">import</span> <span class="pl-s1">pyplot</span> <span class="pl-k">as</span> <span class="pl-s1">plt</span>
<span class="pl-s1">plt</span>.<span class="pl-en">plot</span>([<span class="pl-c1">1</span>, <span class="pl-c1">2</span>], [<span class="pl-c1">1</span>, <span class="pl-c1">2</span>])</pre></div>
<p dir="auto">Will produce the following traceback:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="---------------------------------------------------------------------------
TclError                                  Traceback (most recent call last)
&lt;ipython-input-2-8dd816f9ddc7&gt; in &lt;module&gt;()
----&gt; 1 plt.plot([1, 2], [1, 2])

/home/goldbaum/.virtualenvs/yt-dev/local/lib/python2.7/site-packages/matplotlib/pyplot.pyc in plot(*args, **kwargs)
   3145 @_autogen_docstring(Axes.plot)
   3146 def plot(*args, **kwargs):
-&gt; 3147     ax = gca()
   3148     # allow callers to override the hold state by passing hold=True|False
   3149     washold = ax.ishold()

/home/goldbaum/.virtualenvs/yt-dev/local/lib/python2.7/site-packages/matplotlib/pyplot.pyc in gca(**kwargs)
    926     matplotlib.figure.Figure.gca : The figure's gca method.
    927     &quot;&quot;&quot;
--&gt; 928     return gcf().gca(**kwargs)
    929
    930 # More ways of creating axes:

/home/goldbaum/.virtualenvs/yt-dev/local/lib/python2.7/site-packages/matplotlib/pyplot.pyc in gcf()
    576         return figManager.canvas.figure
    577     else:
--&gt; 578         return figure()
    579
    580

/home/goldbaum/.virtualenvs/yt-dev/local/lib/python2.7/site-packages/matplotlib/pyplot.pyc in figure(num, figsize, dpi, facecolor, edgecolor, frameon, FigureClass, **kwargs)
    525                                         frameon=frameon,
    526                                         FigureClass=FigureClass,
--&gt; 527                                         **kwargs)
    528
    529         if figLabel:

/home/goldbaum/.virtualenvs/yt-dev/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.pyc in new_figure_manager(num, *args, **kwargs)
     82     FigureClass = kwargs.pop('FigureClass', Figure)
     83     figure = FigureClass(*args, **kwargs)
---&gt; 84     return new_figure_manager_given_figure(num, figure)
     85
     86

/home/goldbaum/.virtualenvs/yt-dev/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.pyc in new_figure_manager_given_figure(num, figure)
     90     &quot;&quot;&quot;
     91     _focus = windowing.FocusManager()
---&gt; 92     window = Tk.Tk()
     93     window.withdraw()
     94

/usr/lib/python2.7/lib-tk/Tkinter.pyc in __init__(self, screenName, baseName, className, useTk, sync, use)
   1765                 baseName = baseName + ext
   1766         interactive = 0
-&gt; 1767         self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
   1768         if useTk:
   1769             self._loadtk()

TclError: no display name and no $DISPLAY environment variable"><pre class="notranslate"><code class="notranslate">---------------------------------------------------------------------------
TclError                                  Traceback (most recent call last)
&lt;ipython-input-2-8dd816f9ddc7&gt; in &lt;module&gt;()
----&gt; 1 plt.plot([1, 2], [1, 2])

/home/goldbaum/.virtualenvs/yt-dev/local/lib/python2.7/site-packages/matplotlib/pyplot.pyc in plot(*args, **kwargs)
   3145 @_autogen_docstring(Axes.plot)
   3146 def plot(*args, **kwargs):
-&gt; 3147     ax = gca()
   3148     # allow callers to override the hold state by passing hold=True|False
   3149     washold = ax.ishold()

/home/goldbaum/.virtualenvs/yt-dev/local/lib/python2.7/site-packages/matplotlib/pyplot.pyc in gca(**kwargs)
    926     matplotlib.figure.Figure.gca : The figure's gca method.
    927     """
--&gt; 928     return gcf().gca(**kwargs)
    929
    930 # More ways of creating axes:

/home/goldbaum/.virtualenvs/yt-dev/local/lib/python2.7/site-packages/matplotlib/pyplot.pyc in gcf()
    576         return figManager.canvas.figure
    577     else:
--&gt; 578         return figure()
    579
    580

/home/goldbaum/.virtualenvs/yt-dev/local/lib/python2.7/site-packages/matplotlib/pyplot.pyc in figure(num, figsize, dpi, facecolor, edgecolor, frameon, FigureClass, **kwargs)
    525                                         frameon=frameon,
    526                                         FigureClass=FigureClass,
--&gt; 527                                         **kwargs)
    528
    529         if figLabel:

/home/goldbaum/.virtualenvs/yt-dev/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.pyc in new_figure_manager(num, *args, **kwargs)
     82     FigureClass = kwargs.pop('FigureClass', Figure)
     83     figure = FigureClass(*args, **kwargs)
---&gt; 84     return new_figure_manager_given_figure(num, figure)
     85
     86

/home/goldbaum/.virtualenvs/yt-dev/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.pyc in new_figure_manager_given_figure(num, figure)
     90     """
     91     _focus = windowing.FocusManager()
---&gt; 92     window = Tk.Tk()
     93     window.withdraw()
     94

/usr/lib/python2.7/lib-tk/Tkinter.pyc in __init__(self, screenName, baseName, className, useTk, sync, use)
   1765                 baseName = baseName + ext
   1766         interactive = 0
-&gt; 1767         self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
   1768         if useTk:
   1769             self._loadtk()

TclError: no display name and no $DISPLAY environment variable
</code></pre></div>
<p dir="auto">I think it would be much nicer if backends somehow detected these sorts of errors, fell back to the Agg backend (probably with a warning) and then goes ahead and makes the plot.</p>