.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_plotting/plot_colormaps.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_01_plotting_plot_colormaps.py: Colormaps in Nilearn ==================== Here we show some of the colormaps that ship with Nilearn (some adapted from the HCP connectome workbench color maps). This example show also some of the pros and cons of some of those maps. .. GENERATED FROM PYTHON SOURCE LINES 10-18 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from nilearn.plotting import show from nilearn.plotting.cm import _cmap_d as nilearn_cmaps .. GENERATED FROM PYTHON SOURCE LINES 19-21 Let's create a function to help us plot all the colormaps with their names. .. GENERATED FROM PYTHON SOURCE LINES 21-55 .. code-block:: Python def plot_color_gradients(color_maps): """Create figure and adjust figure height to number of colormaps. Adapted from the matplolib documentation. """ gradient = np.linspace(0, 1, 256) gradient = np.vstack((gradient, gradient)) color_map_names = sorted(color_maps) nrows = len(color_map_names) figh = 0.3 + 0.2 + (nrows + (nrows - 1) * 0.1) * 0.22 fig, axs = plt.subplots(nrows=nrows + 1, figsize=(15, figh)) fig.subplots_adjust( top=1 - 0.3 / figh, bottom=0.2 / figh, left=0.22, right=0.99 ) for ax, name in zip(axs, color_map_names): ax.imshow(gradient, aspect="auto", cmap=name) ax.text( -0.01, 0.5, name, va="center", ha="right", fontsize=12, transform=ax.transAxes, ) # Turn off *all* ticks & spines, not just the ones with colormaps. for ax in axs: ax.set_axis_off() .. GENERATED FROM PYTHON SOURCE LINES 56-58 Plot matplotlib color maps -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 58-60 .. code-block:: Python plot_color_gradients(nilearn_cmaps) .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_colormaps_001.png :alt: plot colormaps :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_colormaps_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 61-63 Plot matplotlib color maps -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 63-75 .. code-block:: Python deprecated_cmaps = ["Vega10", "Vega20", "Vega20b", "Vega20c", "spectral"] m_cmaps = [ m for m in plt.cm.datad if not m.endswith("_r") and m not in deprecated_cmaps ] plot_color_gradients(m_cmaps) show() .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_colormaps_002.png :alt: plot colormaps :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_colormaps_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 76-93 Choosing colormaps ------------------ Some of the `cyclic colormaps `_ shipped with nilearn (like ``"cold_hot"``) will have the same values for very large and very small values, making it hard to distinguish 'activations' from 'deactivations'. In this case, you may want to use a proper `diverging colormaps `_ (like ``"RdBu_r"``, the default for many Nilearn plotting functions). Whatever colormap you choose, we recommend you check that it is perceptually uniform (equal steps in data are perceived as equal steps in the color space) (see `the matplotlib documentation ` for more information). .. GENERATED FROM PYTHON SOURCE LINES 93-104 .. code-block:: Python from nilearn.datasets import load_sample_motor_activation_image from nilearn.plotting import plot_stat_map stat_map = load_sample_motor_activation_image() plot_stat_map(stat_map, cmap="RdBu_r", threshold=6, title="diverging colormap") plot_stat_map(stat_map, cmap="cold_hot", threshold=6, title="cyclic colormap") show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_colormaps_003.png :alt: plot colormaps :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_colormaps_003.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_colormaps_004.png :alt: plot colormaps :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_colormaps_004.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 4.912 seconds) **Estimated memory usage:** 122 MB .. _sphx_glr_download_auto_examples_01_plotting_plot_colormaps.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/nilearn/nilearn/0.12.0?urlpath=lab/tree/notebooks/auto_examples/01_plotting/plot_colormaps.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_colormaps.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_colormaps.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_colormaps.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_