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

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

.. _sphx_glr_gallery_scales_logit_demo.py:


===========
Logit scale
===========

Examples of plots with logit axes.

This example visualises how ``set_yscale("logit")`` works on probability plots
by generating three distributions: normal, laplacian, and cauchy in one plot.

The advantage of logit scale is that it effectively spreads out values close to 0 and 1.

In a linear scale plot, probability values near 0 and 1 appear compressed,
making it difficult to see differences in those regions.

In a logit scale plot, the transformation expands these regions,
making the graph cleaner and easier to compare across different probability values.

This makes the logit scale especially useful when visalising probabilities in logistic
regression, classification models, and cumulative distribution functions.

.. GENERATED FROM PYTHON SOURCE LINES 22-76



.. image-sg:: /gallery/scales/images/sphx_glr_logit_demo_001.png
   :alt: logit scale, logit scale, logit scale, logit scale, linear scale, linear scale
   :srcset: /gallery/scales/images/sphx_glr_logit_demo_001.png, /gallery/scales/images/sphx_glr_logit_demo_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. code-block:: Python


    import math

    import matplotlib.pyplot as plt
    import numpy as np

    xmax = 10
    x = np.linspace(-xmax, xmax, 10000)
    cdf_norm = [math.erf(w / np.sqrt(2)) / 2 + 1 / 2 for w in x]
    cdf_laplacian = np.where(x < 0, 1 / 2 * np.exp(x), 1 - 1 / 2 * np.exp(-x))
    cdf_cauchy = np.arctan(x) / np.pi + 1 / 2

    fig, axs = plt.subplots(nrows=3, ncols=2, figsize=(6.4, 8.5))

    # Common part, for the example, we will do the same plots on all graphs
    for i in range(3):
        for j in range(2):
            axs[i, j].plot(x, cdf_norm, label=r"$\mathcal{N}$")
            axs[i, j].plot(x, cdf_laplacian, label=r"$\mathcal{L}$")
            axs[i, j].plot(x, cdf_cauchy, label="Cauchy")
            axs[i, j].legend()
            axs[i, j].grid()

    # First line, logitscale, with standard notation
    axs[0, 0].set(title="logit scale")
    axs[0, 0].set_yscale("logit")
    axs[0, 0].set_ylim(1e-5, 1 - 1e-5)

    axs[0, 1].set(title="logit scale")
    axs[0, 1].set_yscale("logit")
    axs[0, 1].set_xlim(0, xmax)
    axs[0, 1].set_ylim(0.8, 1 - 5e-3)

    # Second line, logitscale, with survival notation (with `use_overline`), and
    # other format display 1/2
    axs[1, 0].set(title="logit scale")
    axs[1, 0].set_yscale("logit", one_half="1/2", use_overline=True)
    axs[1, 0].set_ylim(1e-5, 1 - 1e-5)

    axs[1, 1].set(title="logit scale")
    axs[1, 1].set_yscale("logit", one_half="1/2", use_overline=True)
    axs[1, 1].set_xlim(0, xmax)
    axs[1, 1].set_ylim(0.8, 1 - 5e-3)

    # Third line, linear scale
    axs[2, 0].set(title="linear scale")
    axs[2, 0].set_ylim(0, 1)

    axs[2, 1].set(title="linear scale")
    axs[2, 1].set_xlim(0, xmax)
    axs[2, 1].set_ylim(0.8, 1)

    fig.tight_layout()
    plt.show()


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

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


.. _sphx_glr_download_gallery_scales_logit_demo.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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