
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/scales/scales.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_scales.py>`
        to download the full example code

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

.. _sphx_glr_gallery_scales_scales.py:


======
Scales
======

Illustrate the scale transformations applied to axes, e.g. log, symlog, logit.

The last two examples are examples of using the ``'function'`` scale by
supplying forward and inverse functions for the scale transformation.

.. GENERATED FROM PYTHON SOURCE LINES 11-104

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    from matplotlib.ticker import FixedLocator, NullFormatter

    # Fixing random state for reproducibility
    np.random.seed(19680801)

    # make up some data in the interval ]0, 1[
    y = np.random.normal(loc=0.5, scale=0.4, size=1000)
    y = y[(y > 0) & (y < 1)]
    y.sort()
    x = np.arange(len(y))

    # plot with various axes scales
    fig, axs = plt.subplots(3, 2, figsize=(6, 8), layout='constrained')

    # linear
    ax = axs[0, 0]
    ax.plot(x, y)
    ax.set_yscale('linear')
    ax.set_title('linear')
    ax.grid(True)


    # log
    ax = axs[0, 1]
    ax.plot(x, y)
    ax.set_yscale('log')
    ax.set_title('log')
    ax.grid(True)


    # symmetric log
    ax = axs[1, 1]
    ax.plot(x, y - y.mean())
    ax.set_yscale('symlog', linthresh=0.02)
    ax.set_title('symlog')
    ax.grid(True)

    # logit
    ax = axs[1, 0]
    ax.plot(x, y)
    ax.set_yscale('logit')
    ax.set_title('logit')
    ax.grid(True)


    # Function x**(1/2)
    def forward(x):
        return x**(1/2)


    def inverse(x):
        return x**2


    ax = axs[2, 0]
    ax.plot(x, y)
    ax.set_yscale('function', functions=(forward, inverse))
    ax.set_title('function: $x^{1/2}$')
    ax.grid(True)
    ax.yaxis.set_major_locator(FixedLocator(np.arange(0, 1, 0.2)**2))
    ax.yaxis.set_major_locator(FixedLocator(np.arange(0, 1, 0.2)))


    # Function Mercator transform
    def forward(a):
        a = np.deg2rad(a)
        return np.rad2deg(np.log(np.abs(np.tan(a) + 1.0 / np.cos(a))))


    def inverse(a):
        a = np.deg2rad(a)
        return np.rad2deg(np.arctan(np.sinh(a)))

    ax = axs[2, 1]

    t = np.arange(0, 170.0, 0.1)
    s = t / 2.

    ax.plot(t, s, '-', lw=2)

    ax.set_yscale('function', functions=(forward, inverse))
    ax.set_title('function: Mercator')
    ax.grid(True)
    ax.set_xlim([0, 180])
    ax.yaxis.set_minor_formatter(NullFormatter())
    ax.yaxis.set_major_locator(FixedLocator(np.arange(0, 90, 10)))

    plt.show()




.. image-sg:: /gallery/scales/images/sphx_glr_scales_001.png
   :alt: linear, log, logit, symlog, function: $x^{1/2}$, function: Mercator
   :srcset: /gallery/scales/images/sphx_glr_scales_001.png, /gallery/scales/images/sphx_glr_scales_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 105-118

.. admonition:: References

   The use of the following functions, methods, classes and modules is shown
   in this example:

   - `matplotlib.axes.Axes.set_xscale`
   - `matplotlib.axes.Axes.set_yscale`
   - `matplotlib.axis.Axis.set_major_locator`
   - `matplotlib.scale.LinearScale`
   - `matplotlib.scale.LogScale`
   - `matplotlib.scale.SymmetricalLogScale`
   - `matplotlib.scale.LogitScale`
   - `matplotlib.scale.FuncScale`


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

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


.. _sphx_glr_download_gallery_scales_scales.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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