Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Scipy2013 Sprint: Cleaning examples of api example #2181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Change according @tonysyu suggestions and run PEP8
  • Loading branch information
gabraganca committed Jul 3, 2013
commit 01a5e99770c06fe14816879f8ff85a4fa89c6396
28 changes: 15 additions & 13 deletions 28 examples/subplots_axes_and_figures/fahrenheit_celsius_scales.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,32 @@
import matplotlib.pyplot as plt
import numpy as np

fig, ax1 = plt.subplots() # ax1 is the Fahrenheit scale
ax2 = ax1.twinx() # ax2 is the Celsius scale

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

def convert_ax2_to_celsius(ax1):

def convert_ax_c_to_celsius(ax_f):
"""
Update second axis according with first axis.
"""
y1, y2 = ax1.get_ylim()
ax2.set_ylim(fahrenheit2celsius(y1), fahrenheit2celsius(y2))
ax2.figure.canvas.draw()
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.
ax1.callbacks.connect("ylim_changed", convert_ax2_to_celsius)
ax1.plot(np.linspace(-40, 120, 100))
ax1.set_xlim(0, 100)
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)

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

plt.show()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.