Repository: nilearn/nilearn

Issue:
I can't make the new colormaps (magma, viridis...) plot the colorbar.
Hi,

I can't make the new colormaps (plt.cm.magma, viridis...) plot the colorbar.
The brain plot is done, but I get an exception when plotting the colorbar.
Has anyone had this problem before?
Thanks.

Cheers,
Alex



Discussion:
> I can't make the new colormaps (plt.cm.magma, viridis...) plot the colorbar.
> The brain plot is done, but I get an exception when plotting the colorbar.
> Has anyone had this problem before?

No, but I am not sure that anybody runs bleeding edge matplotlib.

Could you post the traceback here, please.

I am using matplotlib 1.5.2.
Today I am in a rush, but I can look into this another day. Here it goes:

```
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-321-747b3bb54538> in <module>()
     37 
     38     p1 = plot_spatial_map( pet_blobs, bg_img=bg_img, title="FDG-PET IC {}".format(pet_bold_ics[idx][0]),
---> 39                           cut_coords=cutcoords[idx], cmap=plt.cm.viridis, alpha=1, figure=fig, axes=plt.subplot(gs[0]))
     40     p2 = plot_spatial_map(bold_blobs, bg_img=bg_img, title="fMRI IC {}".format(pet_bold_ics[idx][1]),
     41                           cut_coords=cutcoords[idx], cmap=plt.cm.hot, alpha=1, figure=fig, axes=plt.subplot(gs[1]))

<ipython-input-252-cd0b08153002> in plot_spatial_map(spatial_map, cut_coords, bg_img, title, **kwargs)
     29                                  title=title,
     30                                  draw_cross=False,
---> 31                                  **kwargs,
     32                                  )

/home/alexandre/Software/nilearn/nilearn/plotting/img_plotting.py in plot_roi(roi_img, bg_img, cut_coords, output_file, display_mode, figure, axes, title, annotate, draw_cross, black_bg, alpha, cmap, dim, vmin, vmax, **kwargs)
    657                                 resampling_interpolation='nearest',
    658                                 alpha=alpha, cmap=cmap,
--> 659                                 vmin=vmin, vmax=vmax, **kwargs)
    660     return display
    661 

/home/alexandre/Software/nilearn/nilearn/plotting/img_plotting.py in _plot_img_with_bg(img, bg_img, cut_coords, output_file, display_mode, colorbar, figure, axes, title, threshold, annotate, draw_cross, black_bg, vmin, vmax, bg_vmin, bg_vmax, interpolation, display_factory, cbar_vmin, cbar_vmax, **kwargs)
    182                             threshold=threshold, interpolation=interpolation,
    183                             colorbar=colorbar, vmin=vmin, vmax=vmax,
--> 184                             **kwargs)
    185 
    186     if annotate:

/home/alexandre/Software/nilearn/nilearn/plotting/displays.py in add_overlay(self, img, threshold, colorbar, **kwargs)
    593         # `ims` can be empty in some corner cases, look at test_img_plotting.test_outlier_cut_coords.
    594         if colorbar and ims:
--> 595             self._show_colorbar(ims[0].cmap, ims[0].norm, threshold)
    596 
    597         plt.draw_if_interactive()

/home/alexandre/Software/nilearn/nilearn/plotting/displays.py in _show_colorbar(self, cmap, norm, threshold)
    732             return
    733         else:
--> 734             our_cmap = our_cmap.from_list('Custom cmap', cmaplist, our_cmap.N)
    735 
    736         self._cbar = ColorbarBase(

AttributeError: 'ListedColormap' object has no attribute 'from_list'
```


Resolving patch:
diff --git a/nilearn/plotting/displays.py b/nilearn/plotting/displays.py
--- a/nilearn/plotting/displays.py
+++ b/nilearn/plotting/displays.py
@@ -740,7 +740,8 @@ def _show_colorbar(self, cmap, norm, threshold=None):
         if norm.vmin == norm.vmax:  # len(np.unique(data)) == 1 ?
             return
         else:
-            our_cmap = our_cmap.from_list('Custom cmap', cmaplist, our_cmap.N)
+            our_cmap = colors.LinearSegmentedColormap.from_list(
+                'Custom cmap', cmaplist, our_cmap.N)
 
         self._cbar = ColorbarBase(
             self._colorbar_ax, ticks=ticks, norm=norm,


Test patch:
diff --git a/nilearn/plotting/tests/test_displays.py b/nilearn/plotting/tests/test_displays.py
--- a/nilearn/plotting/tests/test_displays.py
+++ b/nilearn/plotting/tests/test_displays.py
@@ -63,3 +63,12 @@ def test_contour_fillings_levels_in_add_contours():
     # without passing levels, should work with default levels from
     # matplotlib
     oslicer.add_contours(img, filled=True)
+
+
+def test_user_given_cmap_with_colorbar():
+    img = load_mni152_template()
+    oslicer = OrthoSlicer(cut_coords=(0, 0, 0))
+
+    # Test with cmap given as a string
+    oslicer.add_overlay(img, cmap='Paired', colorbar=True)
+    oslicer.close()
diff --git a/nilearn/plotting/tests/test_img_plotting.py b/nilearn/plotting/tests/test_img_plotting.py
--- a/nilearn/plotting/tests/test_img_plotting.py
+++ b/nilearn/plotting/tests/test_img_plotting.py
@@ -4,7 +4,9 @@
 import os
 import tempfile
 from functools import partial
+from distutils.version import LooseVersion
 
+import matplotlib
 import matplotlib.pyplot as plt
 import nibabel
 import numpy as np
@@ -896,3 +898,19 @@ def test_plot_stat_map_with_nans():
     plot_epi(img)
     plot_stat_map(img)
     plot_glass_brain(img)
+
+
+def test_plotting_functions_with_cmaps():
+    img = load_mni152_template()
+    # some cmaps such as 'viridis' (the new default in 2.0), 'magma', 'plasma',
+    # and 'inferno' are not supported for older matplotlib version from < 1.5
+    cmaps = ['Paired', 'Set1', 'Set2', 'Set3']
+    for cmap in cmaps:
+        plot_roi(img, cmap=cmap, colorbar=True)
+        plot_stat_map(img, cmap=cmap, colorbar=True)
+        plot_glass_brain(img, cmap=cmap, colorbar=True)
+
+    if LooseVersion(matplotlib.__version__) >= LooseVersion('2.0.0'):
+        plot_stat_map(img, cmap='viridis', colorbar=True)
+
+    plt.close()