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

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

.. _sphx_glr_gallery_subplots_axes_and_figures_invert_axes.py:


=============
Inverted axis
=============

This example demonstrates two ways to invert the direction of an axis:

- If you want to set *explicit axis limits* anyway, e.g. via `~.Axes.set_xlim`, you
  can swap the limit values: ``set_xlim(4, 0)`` instead of ``set_xlim(0, 4)``.
- Use `.Axis.set_inverted` if you only want to invert the axis *without modifying
  the limits*, i.e. keep existing limits or existing autoscaling behavior.

.. GENERATED FROM PYTHON SOURCE LINES 13-37

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    x = np.arange(0.01, 4.0, 0.01)
    y = np.exp(-x)

    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6.4,  4), layout="constrained")
    fig.suptitle('Inverted axis with ...')

    ax1.plot(x, y)
    ax1.set_xlim(4, 0)   # inverted fixed limits
    ax1.set_title('fixed limits: set_xlim(4, 0)')
    ax1.set_xlabel('decreasing x ⟶')
    ax1.grid(True)

    ax2.plot(x, y)
    ax2.xaxis.set_inverted(True)  # inverted axis with autoscaling
    ax2.set_title('autoscaling: set_inverted(True)')
    ax2.set_xlabel('decreasing x ⟶')
    ax2.grid(True)

    plt.show()


.. GENERATED FROM PYTHON SOURCE LINES 38-43

.. tags::

   component: axis
   plot-type: line
   level: beginner


.. _sphx_glr_download_gallery_subplots_axes_and_figures_invert_axes.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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