
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/scales/symlog_demo.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_scales_symlog_demo.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_scales_symlog_demo.py:


============
Symlog scale
============

The symmetric logarithmic scale is an extension of the logarithmic scale that
also covers negative values. As with the logarithmic scale, it is particularly
useful for numerical data that spans a broad range of values, especially when there
are significant differences between the magnitudes of the numbers involved.

Example use of symlog (symmetric log) axis scaling.

.. GENERATED FROM PYTHON SOURCE LINES 13-41

.. code-block:: Python

    import matplotlib.pyplot as plt
    import numpy as np

    dt = 0.01
    x = np.arange(-50.0, 50.0, dt)
    y = np.arange(0, 100.0, dt)

    fig, (ax0, ax1, ax2) = plt.subplots(nrows=3)

    ax0.plot(x, y)
    ax0.set_xscale('symlog')
    ax0.set_ylabel('symlogx')
    ax0.grid()
    ax0.xaxis.grid(which='minor')  # minor grid on too

    ax1.plot(y, x)
    ax1.set_yscale('symlog')
    ax1.set_ylabel('symlogy')

    ax2.plot(x, np.sin(x / 3.0))
    ax2.set_xscale('symlog')
    ax2.set_yscale('symlog', linthresh=0.015)
    ax2.grid()
    ax2.set_ylabel('symlog both')

    fig.tight_layout()
    plt.show()




.. image-sg:: /gallery/scales/images/sphx_glr_symlog_demo_001.png
   :alt: symlog demo
   :srcset: /gallery/scales/images/sphx_glr_symlog_demo_001.png, /gallery/scales/images/sphx_glr_symlog_demo_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 42-48

Linear threshold
----------------
Since each decade on a logarithmic scale covers the same amount of visual space
and there are infinitely many decades between a given number and zero, the symlog
scale must deviate from logarithmic mapping in a small range
*(-linthresh, linthresh)*, so that the range is mapped to a finite visual space.

.. GENERATED FROM PYTHON SOURCE LINES 48-75

.. code-block:: Python



    def format_axes(ax, title=None):
        """A helper function to better visualize properties of the symlog scale."""
        ax.xaxis.get_minor_locator().set_params(subs=[2, 3, 4, 5, 6, 7, 8, 9])
        ax.grid()
        ax.xaxis.grid(which='minor')  # minor grid on too
        linthresh = ax.xaxis.get_transform().linthresh
        linscale = ax.xaxis.get_transform().linscale
        ax.axvspan(-linthresh, linthresh, color='0.9')
        if title:
            ax.set_title(title.format(linthresh=linthresh, linscale=linscale))


    x = np.linspace(-60, 60, 201)
    y = np.linspace(0, 100.0, 201)

    fig, (ax1, ax2) = plt.subplots(nrows=2, layout="constrained")

    ax1.plot(x, y)
    ax1.set_xscale('symlog', linthresh=1)
    format_axes(ax1, title='Linear region: linthresh={linthresh}')

    ax2.plot(x, y)
    ax2.set_xscale('symlog', linthresh=5)
    format_axes(ax2, title='Linear region: linthresh={linthresh}')




.. image-sg:: /gallery/scales/images/sphx_glr_symlog_demo_002.png
   :alt: Linear region: linthresh=1, Linear region: linthresh=5
   :srcset: /gallery/scales/images/sphx_glr_symlog_demo_002.png, /gallery/scales/images/sphx_glr_symlog_demo_002_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 76-86

Generally, *linthresh* should be chosen so that no or only a few
data points are in the linear region. As a rule of thumb,
:math:`linthresh \approx \mathrm{min} |x|`.


Linear scale
------------
Additionally, the *linscale* parameter determines how much visual space should be
used for the linear range. More precisely, it defines the ratio of visual space
of the region (0, linthresh) relative to one decade.

.. GENERATED FROM PYTHON SOURCE LINES 86-97

.. code-block:: Python


    fig, (ax1, ax2) = plt.subplots(nrows=2, layout="constrained")

    ax1.plot(x, y)
    ax1.set_xscale('symlog', linthresh=1)
    format_axes(ax1, title='Linear region: linthresh={linthresh}, linscale={linscale}')

    ax2.plot(x, y)
    ax2.set_xscale('symlog', linthresh=1, linscale=0.1)
    format_axes(ax2, title='Linear region: linthresh={linthresh}, linscale={linscale}')




.. image-sg:: /gallery/scales/images/sphx_glr_symlog_demo_003.png
   :alt: Linear region: linthresh=1, linscale=1, Linear region: linthresh=1, linscale=0.1
   :srcset: /gallery/scales/images/sphx_glr_symlog_demo_003.png, /gallery/scales/images/sphx_glr_symlog_demo_003_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 98-107

The suitable value for linscale depends on the dynamic range of data. As most data
will be outside the linear region, you typically the linear region only to cover
a small fraction of the visual area.

Limitations and alternatives
----------------------------
The coordinate transform used by ``symlog`` has a discontinuous gradient at the
transition between its linear and logarithmic regions. Depending on data and
scaling, this will be more or less obvious in the plot.

.. GENERATED FROM PYTHON SOURCE LINES 107-113

.. code-block:: Python


    fig, ax = plt.subplots()
    ax.plot(x, y)
    ax.set_xscale('symlog', linscale=0.05)
    format_axes(ax, title="Discontinuous gradient at linear/log transition")




.. image-sg:: /gallery/scales/images/sphx_glr_symlog_demo_004.png
   :alt: Discontinuous gradient at linear/log transition
   :srcset: /gallery/scales/images/sphx_glr_symlog_demo_004.png, /gallery/scales/images/sphx_glr_symlog_demo_004_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 114-124

The ``asinh`` axis scale is an alternative transformation that supports a wide
dynamic range with a smooth gradient and thus may avoid such visual artifacts.
See :doc:`/gallery/scales/asinh_demo`.


.. admonition:: References

   - `matplotlib.scale.SymmetricalLogScale`
   - `matplotlib.ticker.SymmetricalLogLocator`
   - `matplotlib.scale.AsinhScale`


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 2.371 seconds)


.. _sphx_glr_download_gallery_scales_symlog_demo.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: symlog_demo.ipynb <symlog_demo.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: symlog_demo.py <symlog_demo.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: symlog_demo.zip <symlog_demo.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
