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

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

.. _sphx_glr_gallery_subplots_axes_and_figures_fahrenheit_celsius_scales.py:


=================================
Different scales on the same Axes
=================================

Demo of how to display two scales on the left and right y-axis.

This example uses the Fahrenheit and Celsius scales.

.. GENERATED FROM PYTHON SOURCE LINES 10-48

.. code-block:: Python

    import matplotlib.pyplot as plt
    import numpy as np


    def fahrenheit2celsius(temp):
        """
        Returns temperature in Celsius given Fahrenheit temperature.
        """
        return (5. / 9.) * (temp - 32)


    def make_plot():

        # Define a closure function to register as a callback
        def convert_ax_c_to_celsius(ax_f):
            """
            Update second axis according to first axis.
            """
            y1, y2 = ax_f.get_ylim()
            ax_c.set_ylim(fahrenheit2celsius(y1), fahrenheit2celsius(y2))
            ax_c.figure.canvas.draw()

        fig, ax_f = plt.subplots()
        ax_c = ax_f.twinx()

        # automatically update ylim of ax2 when ylim of ax1 changes.
        ax_f.callbacks.connect("ylim_changed", convert_ax_c_to_celsius)
        ax_f.plot(np.linspace(-40, 120, 100))
        ax_f.set_xlim(0, 100)

        ax_f.set_title('Two scales: Fahrenheit and Celsius')
        ax_f.set_ylabel('Fahrenheit')
        ax_c.set_ylabel('Celsius')

        plt.show()

    make_plot()


.. GENERATED FROM PYTHON SOURCE LINES 49-54

.. tags::

   component: axes
   plot-type: line
   level: beginner


.. _sphx_glr_download_gallery_subplots_axes_and_figures_fahrenheit_celsius_scales.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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