You are reading an old version of the documentation (v1.5.1). For the latest version see

We're updating the default styles for Matplotlib 2.0

Learn what to expect in the new updates



This Page

subplots_axes_and_figures example code: fahrenheit_celsius_scales.pyΒΆ

(Source code, png, hires.png, pdf)

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

This example uses the Fahrenheit and Celsius scales.
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))

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')

Keywords: python, matplotlib, pylab, example, codex (see Search examples)