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

Skip to content
Closed
Show file tree
Hide file tree
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
Next Next commit
Clean two scaled y-axis demo.
  • Loading branch information
gabraganca committed Jun 29, 2013
commit fcc48f59aca2eeea9351d019c7fae5e45fd74693
27 changes: 0 additions & 27 deletions examples/api/fahrenheit_celsius_scales.py

This file was deleted.

35 changes: 35 additions & 0 deletions examples/subplots_axes_and_figures/fahrenheit_celsius_scales.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really Minor: Maybe move figure/axes definitions down with the rest of the plotting code (i.e. below the function defs). Also, I think the inline comments should be removed here. (Maybe ax1 -> ax_f, ax2 -> ax_c?)


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

def convert_ax2_to_celsius(ax1):
"""
Update second axis according with first axis.
"""
y1, y2 = ax1.get_ylim()
ax2.set_ylim(fahrenheit2celsius(y1), fahrenheit2celsius(y2))
ax2.figure.canvas.draw()

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

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

plt.show()