{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Symlog scale\n\nThe symmetric logarithmic scale is an extension of the logarithmic scale that\nalso covers negative values. As with the logarithmic scale, it is particularly\nuseful for numerical data that spans a broad range of values, especially when there\nare significant differences between the magnitudes of the numbers involved.\n\nExample use of symlog (symmetric log) axis scaling.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\ndt = 0.01\nx = np.arange(-50.0, 50.0, dt)\ny = np.arange(0, 100.0, dt)\n\nfig, (ax0, ax1, ax2) = plt.subplots(nrows=3)\n\nax0.plot(x, y)\nax0.set_xscale('symlog')\nax0.set_ylabel('symlogx')\nax0.grid()\nax0.xaxis.grid(which='minor')  # minor grid on too\n\nax1.plot(y, x)\nax1.set_yscale('symlog')\nax1.set_ylabel('symlogy')\n\nax2.plot(x, np.sin(x / 3.0))\nax2.set_xscale('symlog')\nax2.set_yscale('symlog', linthresh=0.015)\nax2.grid()\nax2.set_ylabel('symlog both')\n\nfig.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Linear threshold\nSince each decade on a logarithmic scale covers the same amount of visual space\nand there are infinitely many decades between a given number and zero, the symlog\nscale must deviate from logarithmic mapping in a small range\n*(-linthresh, linthresh)*, so that the range is mapped to a finite visual space.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def format_axes(ax, title=None):\n    \"\"\"A helper function to better visualize properties of the symlog scale.\"\"\"\n    ax.xaxis.get_minor_locator().set_params(subs=[2, 3, 4, 5, 6, 7, 8, 9])\n    ax.grid()\n    ax.xaxis.grid(which='minor')  # minor grid on too\n    linthresh = ax.xaxis.get_transform().linthresh\n    linscale = ax.xaxis.get_transform().linscale\n    ax.axvspan(-linthresh, linthresh, color='0.9')\n    if title:\n        ax.set_title(title.format(linthresh=linthresh, linscale=linscale))\n\n\nx = np.linspace(-60, 60, 201)\ny = np.linspace(0, 100.0, 201)\n\nfig, (ax1, ax2) = plt.subplots(nrows=2, layout=\"constrained\")\n\nax1.plot(x, y)\nax1.set_xscale('symlog', linthresh=1)\nformat_axes(ax1, title='Linear region: linthresh={linthresh}')\n\nax2.plot(x, y)\nax2.set_xscale('symlog', linthresh=5)\nformat_axes(ax2, title='Linear region: linthresh={linthresh}')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Generally, *linthresh* should be chosen so that no or only a few\ndata points are in the linear region. As a rule of thumb,\n$linthresh \\approx \\mathrm{min} |x|$.\n\n\n## Linear scale\nAdditionally, the *linscale* parameter determines how much visual space should be\nused for the linear range. More precisely, it defines the ratio of visual space\nof the region (0, linthresh) relative to one decade.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, (ax1, ax2) = plt.subplots(nrows=2, layout=\"constrained\")\n\nax1.plot(x, y)\nax1.set_xscale('symlog', linthresh=1)\nformat_axes(ax1, title='Linear region: linthresh={linthresh}, linscale={linscale}')\n\nax2.plot(x, y)\nax2.set_xscale('symlog', linthresh=1, linscale=0.1)\nformat_axes(ax2, title='Linear region: linthresh={linthresh}, linscale={linscale}')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The suitable value for linscale depends on the dynamic range of data. As most data\nwill be outside the linear region, you typically the linear region only to cover\na small fraction of the visual area.\n\n## Limitations and alternatives\nThe coordinate transform used by ``symlog`` has a discontinuous gradient at the\ntransition between its linear and logarithmic regions. Depending on data and\nscaling, this will be more or less obvious in the plot.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, ax = plt.subplots()\nax.plot(x, y)\nax.set_xscale('symlog', linscale=0.05)\nformat_axes(ax, title=\"Discontinuous gradient at linear/log transition\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The ``asinh`` axis scale is an alternative transformation that supports a wide\ndynamic range with a smooth gradient and thus may avoid such visual artifacts.\nSee :doc:`/gallery/scales/asinh_demo`.\n\n\n.. admonition:: References\n\n   - `matplotlib.scale.SymmetricalLogScale`\n   - `matplotlib.ticker.SymmetricalLogLocator`\n   - `matplotlib.scale.AsinhScale`\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.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}