Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0fad023

Browse files
committed
Merge pull request #2784 from gabraganca/fc_example
Scipy2013 Sprint: Cleaning F/C example
2 parents 8aec3fb + 53d68db commit 0fad023

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

examples/api/fahrenheit_celsius_scales.py

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Demo of how to display two scales on the left and right y axis.
3+
4+
This example uses the Fahrenheit and Celsius scales.
5+
"""
6+
import matplotlib.pyplot as plt
7+
import numpy as np
8+
9+
10+
def fahrenheit2celsius(temp):
11+
"""
12+
Returns temperature in Celsius.
13+
"""
14+
return (5. / 9.) * (temp - 32)
15+
16+
17+
def convert_ax_c_to_celsius(ax_f):
18+
"""
19+
Update second axis according with first axis.
20+
"""
21+
y1, y2 = ax_f.get_ylim()
22+
ax_c.set_ylim(fahrenheit2celsius(y1), fahrenheit2celsius(y2))
23+
ax_c.figure.canvas.draw()
24+
25+
fig, ax_f = plt.subplots()
26+
ax_c = ax_f.twinx()
27+
28+
# automatically update ylim of ax2 when ylim of ax1 changes.
29+
ax_f.callbacks.connect("ylim_changed", convert_ax_c_to_celsius)
30+
ax_f.plot(np.linspace(-40, 120, 100))
31+
ax_f.set_xlim(0, 100)
32+
33+
ax_f.set_title('Two scales: Fahrenheit and Celsius')
34+
ax_f.set_ylabel('Fahrenheit')
35+
ax_c.set_ylabel('Celsius')
36+
37+
plt.show()

0 commit comments

Comments
 (0)