Repository: nilearn/nilearn

Issue:
display.add_edges could be made more 'fluid'
While playing interactively with display.add_edges in ipython -pylab, I note the two following 'glitches':

``` python
from nilearn import datasets, plotting, image
data = datasets.fetch_adhd()
mean_func = image.mean_img(data.func[1])
display = plotting.plot_epi(mean_func)
display.add_edges(image.mean_img(data.func[1]), color='w')
```

**Glitch 1:** once all the lines are executed the plot is not refreshed, to have the contour, I need to call manually 'plt.draw()'. We probably need to add a 'plt.draw_if_interactive()' at the end of the add_edges method (and we should check whether it is necessary for add_contour and add_overlay)

**Glitch 2:** In this setting, the alpha is too low, and thus the edges are not visible. I am in favor of removing the alpha (which I implemented in a hacky way with an alpha_cmap, and simply using a hard color).



Discussion:
I noticed Glitch 1 for add_overlay so I am guessing the draw_if_interactive needs to be added to all the add_ functions.

About Glitch 2, just to make sure I read over the code correctly the edge array is supposed to contain only a single value?

> I noticed Glitch 1 for add_overlay so I am guessing the draw_if_interactive
> needs to be added to all the add_ functions.

I would guess too.

> About Glitch 2, just to make sure I read over the code correctly the
> edge array is supposed to contain only a single value?

I believe so.


Resolving patch:
diff --git a/nilearn/plotting/displays.py b/nilearn/plotting/displays.py
--- a/nilearn/plotting/displays.py
+++ b/nilearn/plotting/displays.py
@@ -15,7 +15,7 @@
 
 try:
     import pylab as pl
-    from matplotlib import transforms
+    from matplotlib import transforms, colors
     from matplotlib.colorbar import ColorbarBase
 except ImportError:
     skip_if_running_nose('Could not import matplotlib')
@@ -24,7 +24,6 @@
 # Local imports
 from .find_cuts import find_xyz_cut_coords, find_cut_slices
 from .edge_detect import _edge_map
-from . import cm
 from ..image.resampling import get_bounds, reorder_img, coord_transform,\
             get_mask_bounds
 
@@ -407,6 +406,8 @@ def add_overlay(self, img, threshold=1e-6, colorbar=False, **kwargs):
         if colorbar:
             self._colorbar_show(ims[0], threshold)
 
+        pl.draw_if_interactive()
+
     def add_contours(self, img, **kwargs):
         """ Contour a 3D map in all the views.
 
@@ -424,6 +425,7 @@ def add_contours(self, img, **kwargs):
                 these contours.
         """
         self._map_show(img, type='contour', **kwargs)
+        pl.draw_if_interactive()
 
     def _map_show(self, img, type='imshow', **kwargs):
         img = reorder_img(img)
@@ -537,7 +539,7 @@ def add_edges(self, img, color='r'):
         img = reorder_img(img)
         data = img.get_data()
         affine = img.get_affine()
-        kwargs = dict(cmap=cm.alpha_cmap(color=color))
+        single_color_cmap = colors.ListedColormap([color])
         data_bounds = get_bounds(data.shape, img.get_affine())
 
         # For each ax, cut the data and plot it
@@ -549,7 +551,9 @@ def add_edges(self, img, color='r'):
                 # We are cutting outside the indices of the data
                 continue
             display_ax.draw_2d(edge_mask, data_bounds, data_bounds,
-                               type='imshow', **kwargs)
+                               type='imshow', cmap=single_color_cmap)
+
+        pl.draw_if_interactive()
 
     def annotate(self, left_right=True, positions=True, size=12, **kwargs):
         """ Add annotations to the plot.


Test patch: