{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Asinh scale\n\nIllustration of the `asinh <.scale.AsinhScale>` axis scaling,\nwhich uses the transformation\n\n\\begin{align}a \\rightarrow a_0 \\sinh^{-1} (a / a_0)\\end{align}\n\nFor coordinate values close to zero (i.e. much smaller than\nthe \"linear width\" $a_0$), this leaves values essentially unchanged:\n\n\\begin{align}a \\rightarrow a + \\mathcal{O}(a^3)\\end{align}\n\nbut for larger values (i.e. $|a| \\gg a_0$, this is asymptotically\n\n\\begin{align}a \\rightarrow a_0 \\, \\mathrm{sgn}(a) \\ln |a| + \\mathcal{O}(1)\\end{align}\n\nAs with the `symlog <.scale.SymmetricalLogScale>` scaling,\nthis allows one to plot quantities\nthat cover a very wide dynamic range that includes both positive\nand negative values. However, ``symlog`` involves a transformation\nthat has discontinuities in its gradient because it is built\nfrom *separate* linear and logarithmic transformations.\nThe ``asinh`` scaling uses a transformation that is smooth\nfor all (finite) values, which is both mathematically cleaner\nand reduces visual artifacts associated with an abrupt\ntransition between linear and logarithmic regions of the plot.\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>`.scale.AsinhScale` is experimental, and the API may change.</p></div>\n\nSee `~.scale.AsinhScale`, `~.scale.SymmetricalLogScale`.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\n# Prepare sample values for variations on y=x graph:\nx = np.linspace(-3, 6, 500)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Compare \"symlog\" and \"asinh\" behaviour on sample y=x graph,\nwhere there is a discontinuous gradient in \"symlog\" near y=2:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig1 = plt.figure()\nax0, ax1 = fig1.subplots(1, 2, sharex=True)\n\nax0.plot(x, x)\nax0.set_yscale('symlog')\nax0.grid()\nax0.set_title('symlog')\n\nax1.plot(x, x)\nax1.set_yscale('asinh')\nax1.grid()\nax1.set_title('asinh')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Compare \"asinh\" graphs with different scale parameter \"linear_width\":\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig2 = plt.figure(layout='constrained')\naxs = fig2.subplots(1, 3, sharex=True)\nfor ax, (a0, base) in zip(axs, ((0.2, 2), (1.0, 0), (5.0, 10))):\n    ax.set_title(f'linear_width={a0:.3g}')\n    ax.plot(x, x, label='y=x')\n    ax.plot(x, 10*x, label='y=10x')\n    ax.plot(x, 100*x, label='y=100x')\n    ax.set_yscale('asinh', linear_width=a0, base=base)\n    ax.grid()\n    ax.legend(loc='best', fontsize='small')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Compare \"symlog\" and \"asinh\" scalings\non 2D Cauchy-distributed random numbers,\nwhere one may be able to see more subtle artifacts near y=2\ndue to the gradient-discontinuity in \"symlog\":\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig3 = plt.figure()\nax = fig3.subplots(1, 1)\nr = 3 * np.tan(np.random.uniform(-np.pi / 2.02, np.pi / 2.02,\n                                 size=(5000,)))\nth = np.random.uniform(0, 2*np.pi, size=r.shape)\n\nax.scatter(r * np.cos(th), r * np.sin(th), s=4, alpha=0.5)\nax.set_xscale('asinh')\nax.set_yscale('symlog')\nax.set_xlabel('asinh')\nax.set_ylabel('symlog')\nax.set_title('2D Cauchy random deviates')\nax.set_xlim(-50, 50)\nax.set_ylim(-50, 50)\nax.grid()\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. admonition:: References\n\n   - `matplotlib.scale.AsinhScale`\n   - `matplotlib.ticker.AsinhLocator`\n   - `matplotlib.scale.SymmetricalLogScale`\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.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}