File: fahrenheit_celcius_scales.py

package info (click to toggle)
matplotlib 1.1.1~rc2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 66,076 kB
  • sloc: python: 90,600; cpp: 69,891; objc: 5,231; ansic: 1,723; makefile: 171; sh: 7
file content (28 lines) | stat: -rw-r--r-- 662 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""
Shoiw how to display two scales on the left and right y axis -- Fahrenheit and Celcius
"""

import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)     # the Fahrenheit scale
ax2 = ax1.twinx()              # the Celcius scale

def Tc(Tf):
    return (5./9.)*(Tf-32)


def update_ax2(ax1):
   y1, y2 = ax1.get_ylim()
   ax2.set_ylim(Tc(y1), Tc(y2))
   ax2.figure.canvas.draw()

# automatically update ylim of ax2 when ylim of ax1 changes.
ax1.callbacks.connect("ylim_changed", update_ax2)
ax1.plot([78, 79, 79, 77])

ax1.set_title('Two scales: Fahrenheit and Celcius')
ax1.set_ylabel('Fahrenheit')
ax2.set_ylabel('Celcius')

plt.show()