Repository: nilearn/nilearn

Issue:
Plots won't show up
Hi everyone,

using nilearn on OSX El Capitan, when executing the example scripts like plot_demo_glass_brain.py no plots will show up. PiP and iPython linked to the same folders, matplotlib alone does show plots. All dependencies are up-to-date and installed. Don't really know how to fix this.



Discussion:
How do you execute these scripts?

I tried "python script_name.py" and "ipython script_name.py", respectively

OK, "Python script_name.py" should work, but it might be an OSX thing, I don't know anything about OSX.

How do you get matplotlib to show plots?

I just tested trivial things in the ipython environment like:

```
import matplotlib.pyplot as plt
plt.plot(range(10), range(10))
plt.show()
```

just to test whether matplotlib works properly

Then just run the example using "%run filename.py" in the IPython
environnement where things work well.

Ok, didn't try to run it from within python. Now plots show, but I also get following error:

```
/Users/Stephan/anaconda/lib/python2.7/site-packages/matplotlib/__init__.py:1350: 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.
```

plots only show when I open ipython via `ipython --matplotlib` but then I get the error from above. When I open it with without `--matplotlib` flag, no plots and no error

Out of curiosity, can you tell use what do the following return in
python:

<pre>
import os
print(os.name)
print('DISPLAY' in os.environ')
</pre>

sure:

```
In [1]: import os

In [2]: print os.name
posix

In [4]: print('DISPLAY' in os.environ)
False
```

I am starting to understand better what you are experiencing. Can you
also give us the output of:

<pre>
print(os.uname())
import matplotlib
print(matplotlib.get_backend())
</pre>

cool, thanks for your help man. here is the output:

```
In [5]: print(os.uname())
('Darwin', 'KH-Lees-Macbook.local', '15.0.0', 'Darwin Kernel Version 15.0.0: Sat Sep 19 15:53:46 PDT 2015; root:xnu-3247.10.11~1/RELEASE_X86_64', 'x86_64')

In [7]: print(matplotlib.get_backend())
MacOSX
```

OK. You can work in the "ipython --matplotlib" environment; it won't be any problem.

The rest are things that we can fix based on your feedback, so that it works seemlessly in all situations. We'll release a fix soon; Thanks!

Many thanks, have a good day!

Note for self (I know how to fix this, and will submit a PR soon): sampling of MacOSX installs:
- os.name is always posix
- os.uname()[0] is always 'Darwin':
- "'DISPLAY' in os.environ": True 2 out of 3
- matplotlib.get_backend():
  - 'TkAgg' (on @ogrisel's computer)
  -  'MacOSX' on the two others

Also:

``` python
sys.platform == 'darwin'
```

this is the canonical way to detect OSX.

> sys.platform == 'darwin'
> 
> this is the canonical way to detect OSX.

Thanks! I was missing that.


Resolving patch:
diff --git a/nilearn/plotting/__init__.py b/nilearn/plotting/__init__.py
--- a/nilearn/plotting/__init__.py
+++ b/nilearn/plotting/__init__.py
@@ -11,8 +11,12 @@ def _set_mpl_backend():
         # We are doing local imports here to avoid poluting our namespace
         import matplotlib
         import os
+        import sys
         # Set the backend to a non-interactive one for unices without X
-        if os.name == 'posix' and 'DISPLAY' not in os.environ:
+        if (os.name == 'posix' and 'DISPLAY' not in os.environ
+            and not (sys.platform == 'darwin'
+                     and matplotlib.get_backend() == 'MacOSX'
+                     )):
             matplotlib.use('Agg')
     except ImportError:
         from .._utils.testing import skip_if_running_nose


Test patch: