{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Colormap normalizations SymLogNorm\n\nDemonstration of using norm to map colormaps onto data in non-linear ways.\n\n.. redirect-from:: /gallery/userdemo/colormap_normalization_symlognorm\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Synthetic dataset consisting of two humps, one negative and one positive,\nthe positive with 8-times the amplitude.\nLinearly, the negative hump is almost invisible,\nand it is very difficult to see any detail of its profile.\nWith the logarithmic scaling applied to both positive and negative values,\nit is much easier to see the shape of each hump.\n\nSee `~.colors.SymLogNorm`.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport matplotlib.colors as colors\n\n\ndef rbf(x, y):\n    return 1.0 / (1 + 5 * ((x ** 2) + (y ** 2)))\n\nN = 200\ngain = 8\nX, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]\nZ1 = rbf(X + 0.5, Y + 0.5)\nZ2 = rbf(X - 0.5, Y - 0.5)\nZ = gain * Z1 - Z2\n\nshadeopts = {'cmap': 'PRGn', 'shading': 'gouraud'}\ncolormap = 'PRGn'\nlnrwidth = 0.5\n\nfig, ax = plt.subplots(2, 1, sharex=True, sharey=True)\n\npcm = ax[0].pcolormesh(X, Y, Z,\n                       norm=colors.SymLogNorm(linthresh=lnrwidth, linscale=1,\n                                              vmin=-gain, vmax=gain, base=10),\n                       **shadeopts)\nfig.colorbar(pcm, ax=ax[0], extend='both')\nax[0].text(-2.5, 1.5, 'symlog')\n\npcm = ax[1].pcolormesh(X, Y, Z, vmin=-gain, vmax=gain,\n                       **shadeopts)\nfig.colorbar(pcm, ax=ax[1], extend='both')\nax[1].text(-2.5, 1.5, 'linear')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "In order to find the best visualization for any particular dataset,\nit may be necessary to experiment with multiple different color scales.\nAs well as the `~.colors.SymLogNorm` scaling, there is also\nthe option of using `~.colors.AsinhNorm` (experimental), which has a smoother\ntransition between the linear and logarithmic regions of the transformation\napplied to the data values, \"Z\".\nIn the plots below, it may be possible to see contour-like artifacts\naround each hump despite there being no sharp features\nin the dataset itself. The ``asinh`` scaling shows a smoother shading\nof each hump.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, ax = plt.subplots(2, 1, sharex=True, sharey=True)\n\npcm = ax[0].pcolormesh(X, Y, Z,\n                       norm=colors.SymLogNorm(linthresh=lnrwidth, linscale=1,\n                                              vmin=-gain, vmax=gain, base=10),\n                       **shadeopts)\nfig.colorbar(pcm, ax=ax[0], extend='both')\nax[0].text(-2.5, 1.5, 'symlog')\n\npcm = ax[1].pcolormesh(X, Y, Z,\n                       norm=colors.AsinhNorm(linear_width=lnrwidth,\n                                             vmin=-gain, vmax=gain),\n                       **shadeopts)\nfig.colorbar(pcm, ax=ax[1], extend='both')\nax[1].text(-2.5, 1.5, 'asinh')\n\n\nplt.show()"
      ]
    }
  ],
  "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.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}