{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# Tutorial 5: Colors and colorbars\n\nThis tutorial demonstrates how to configure the colorbar(s) with ``surfplot``.\n\n## Layer color maps and colorbars \n\nThe color map can be specified for each added plotting layer using the `cmap` \nparameter of :func:`~surfplot.plotting.Plot.add_layer`, along with the \nassociated ``matplotlib`` colorbar drawn if specified. The colobar can be \nturned off by `cbar=False`. The range of the colormap is specified with the \n`color_range` parameter, which takes a tuple of (`minimum`, `maximum`) values. \nIf no color range is specified (the default, i.e. `None`), then the color range \nis computed automically based on the minimum and maximum of the data.\n\nLet's get started by setting up a plot with surface shading added as well. \nFollowing the first initial steps of \n`sphx_glr_auto_examples_plot_tutorial_01.py` :\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from neuromaps.datasets import fetch_fslr\nfrom surfplot import Plot\n\nsurfaces = fetch_fslr()\nlh, rh = surfaces['inflated']\np = Plot(lh, rh)\n\nsulc_lh, sulc_rh = surfaces['sulc']\np.add_layer({'left': sulc_lh, 'right': sulc_rh}, cmap='binary_r', cbar=False)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now let's add a plotting layer with a colorbar using the example data. The\n`cmap` parameter accepts any named `matplotlib colormap`_, or a \n`colormap object`_. This means that ``surfplot`` can work with pretty much\nany colormap, including those from `seaborn`_ and `cmasher`_,  for example.  \n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from surfplot.datasets import load_example_data\n\n# default mode network associations\ndefault = load_example_data(join=True)\np.add_layer(default, cmap='GnBu_r', cbar_label='Default mode')\nfig = p.build()\nfig.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "`cbar_label` added a text label to the colorbar. Although not necessary in\ncases where a single layer/colorbar is shown, it can be useful when adding\nmultiple layers. To demonstrate that, let's add another layer using the\n`frontoparietal` network associations from \n:func:`~surfplot.datasets.load_example_data`:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fronto = load_example_data('frontoparietal', join=True)\np.add_layer(fronto, cmap='YlOrBr_r', cbar_label='Frontoparietal')\nfig = p.build()\nfig.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The order of the colorbars is always based on the order of the layers, where \nthe outermost colorbar is the last (i.e. uppermost) plotting layer. Of \ncourse, more layers and colorbars can lead to busy-looking figure, so be sure\nnot to overdo it. \n\n## cbar_kws\n\nOnce all layers have been added, the positioning and style can be adjusted \nusing the `cbar_kws` parameter in :func:`~surfplot.plotting.Plot.build`, \nwhich are keyword arguments for :func:`surfplot.plotting.Plot._add_colorbars`. \nEach one is briefly described below (see :func:`~surfplot.plotting.Plot._add_colorbars`\nfor more detail):\n\n1. `location`: The location, relative to the surface plot\n2. `label_direction`: Angle to draw label for colorbars\n3. `n_ticks`: Number of ticks to include on colorbar\n4. `decimals`: Number of decimals to show for colorbal tick values\n5. `fontsize`: Font size for colorbar labels and tick labels\n6. `draw_border`: Draw ticks and black border around colorbar\n7. `outer_labels_only`: Show tick labels for only the outermost colorbar\n8. `aspect`: Ratio of long to short dimensions\n9. `pad`: Space that separates each colorbar\n10. `shrink`: Fraction by which to multiply the size of the colorbar\n11. `fraction`: Fraction of original axes to use for colorbar\n\nLet's plot colorbars on the right, which will generate vertical colorbars \ninstead of horizontal colorbars. We'll also add some style changes for a \ncleaner look: \n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "kws = {'location': 'right', 'label_direction': 45, 'decimals': 1, \n       'fontsize': 8, 'n_ticks': 2, 'shrink': .15, 'aspect': 8, \n       'draw_border': False}\nfig = p.build(cbar_kws=kws)\nfig.show()\n# sphinx_gallery_thumbnail_number = 3"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Be sure to check out `sphx_glr_auto_examples_examples_plot_example_01.py`\nfor another example of colorbar styling.\n\n## Transparency\n\nThe transparency of the plotting layers can be adjusted by the ``alpha`` \nparameter. This may be preferred in cases with overlapping plotting layers.\nWe can recreate the example above with transparent maps like so:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "p = Plot(lh, rh)\np.add_layer({'left': sulc_lh, 'right': sulc_rh}, cmap='binary_r', cbar=False)\np.add_layer(default, cmap='GnBu_r', cbar_label='Default mode', alpha=.5)\np.add_layer(fronto, cmap='YlOrBr_r', cbar_label='Frontoparietal', alpha=.5)\nfig = p.build(cbar_kws=kws)\nfig.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Although these particular maps are largely non-overlapping, you can see some\nsmall overlap at the edges of the  default mode and frontoparietal clusters \nthanks to the transparency.\n\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.15"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}