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

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

.. _sphx_glr_gallery_subplots_axes_and_figures_two_scales.py:


===========================
Plots with different scales
===========================

Two plots on the same Axes with different left and right scales.

The trick is to use *two different Axes* that share the same *x* axis.
You can use separate `matplotlib.ticker` formatters and locators as
desired since the two Axes are independent.

Such Axes are generated by calling the `.Axes.twinx` method. Likewise,
`.Axes.twiny` is available to generate Axes that share a *y* axis but
have different top and bottom scales.

.. GENERATED FROM PYTHON SOURCE LINES 16-42

.. code-block:: Python

    import matplotlib.pyplot as plt
    import numpy as np

    # Create some mock data
    t = np.arange(0.01, 10.0, 0.01)
    data1 = np.exp(t)
    data2 = np.sin(2 * np.pi * t)

    fig, ax1 = plt.subplots()

    color = 'tab:red'
    ax1.set_xlabel('time (s)')
    ax1.set_ylabel('exp', color=color)
    ax1.plot(t, data1, color=color)
    ax1.tick_params(axis='y', labelcolor=color)

    ax2 = ax1.twinx()  # instantiate a second Axes that shares the same x-axis

    color = 'tab:blue'
    ax2.set_ylabel('sin', color=color)  # we already handled the x-label with ax1
    ax2.plot(t, data2, color=color)
    ax2.tick_params(axis='y', labelcolor=color)

    fig.tight_layout()  # otherwise the right y-label is slightly clipped
    plt.show()


.. GENERATED FROM PYTHON SOURCE LINES 43-57

.. admonition:: References

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

   - `matplotlib.axes.Axes.twinx` / `matplotlib.pyplot.twinx`
   - `matplotlib.axes.Axes.twiny` / `matplotlib.pyplot.twiny`
   - `matplotlib.axes.Axes.tick_params` / `matplotlib.pyplot.tick_params`

.. tags::

   component: axes
   plot-type: line
   level: beginner


.. _sphx_glr_download_gallery_subplots_axes_and_figures_two_scales.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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