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

    Click :ref:`here <sphx_glr_download_gallery_scales_log_demo.py>` to download the full example code
.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_scales_log_demo.py:


========
Log Demo
========

Examples of plots with logarithmic axes.




.. image:: /gallery/scales/images/sphx_glr_log_demo_001.png
    :class: sphx-glr-single-img





.. code-block:: python


    import numpy as np
    import matplotlib.pyplot as plt

    # Data for plotting
    t = np.arange(0.01, 20.0, 0.01)

    # Create figure
    fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)

    # log y axis
    ax1.semilogy(t, np.exp(-t / 5.0))
    ax1.set(title='semilogy')
    ax1.grid()

    # log x axis
    ax2.semilogx(t, np.sin(2 * np.pi * t))
    ax2.set(title='semilogx')
    ax2.grid()

    # log x and y axis
    ax3.loglog(t, 20 * np.exp(-t / 10.0), basex=2)
    ax3.set(title='loglog base 2 on x')
    ax3.grid()

    # With errorbars: clip non-positive values
    # Use new data for plotting
    x = 10.0**np.linspace(0.0, 2.0, 20)
    y = x**2.0

    ax4.set_xscale("log", nonposx='clip')
    ax4.set_yscale("log", nonposy='clip')
    ax4.set(title='Errorbars go negative')
    ax4.errorbar(x, y, xerr=0.1 * x, yerr=5.0 + 0.75 * y)
    # ylim must be set after errorbar to allow errorbar to autoscale limits
    ax4.set_ylim(ymin=0.1)

    fig.tight_layout()
    plt.show()


.. _sphx_glr_download_gallery_scales_log_demo.py:


.. only :: html

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



  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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


.. only:: html

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

    Keywords: matplotlib code example, codex, python plot, pyplot
    `Gallery generated by Sphinx-Gallery
    <https://sphinx-gallery.readthedocs.io>`_
