

.. _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.




.. image:: /gallery/subplots_axes_and_figures/images/sphx_glr_fahrenheit_celsius_scales_001.png
    :align: center





.. code-block:: python

    import matplotlib.pyplot as plt
    import numpy as np


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


    def convert_ax_c_to_celsius(ax_f):
        """
        Update second axis according with 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()

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



.. container:: sphx-glr-footer


  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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

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

    `Generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
