api example code: two_scales.py — Matplotlib 1.5.1 documentation
You are reading an old version of the documentation (v1.5.1). For the latest version see https://matplotlib.org/stable/ We're updating the default styles for Matplotlib 2.0 Learn what to expect in the new updates Navigation index modules | next | previous | home | examples | gallery | pyplot | docs » Matplotlib Examples » api Examples » Travis-CI: Related Topics Documentation overview Matplotlib Examples api Examples Previous: api example code: span_regions.py Next: api example code: unicode_minus.py This Page Show Source Quick search Enter search terms or a module, class or function name. api example code: two_scales.py ¶ ( Source code , png , hires.png , pdf ) #!/usr/bin/env python """ Demonstrate how to do two plots on the same axes with different left right scales.
The trick is to use *2 different axes*. Turn the axes rectangular frame off on the 2nd axes to keep it from obscuring the first. Manually set the tick locs and labels as desired. You can use separate matplotlib.ticker formatters and locators as desired since the two axes are independent. This is achieved in the following example by calling the Axes.twinx() method, which performs this work. See the source of twinx() in axes.py for an example of how to do it for different x scales. (Hint: use the xaxis instance and call tick_bottom and tick_top in place of tick_left and tick_right.) The twinx and twiny methods are also exposed as pyplot functions.
""" import numpy as np import matplotlib.pyplot as plt fig , ax1 = plt . subplots () t = np . arange ( 0.01 , 10.0 , 0.01 ) s1 = np . exp ( t ) ax1 . plot ( t , s1 , 'b-' ) ax1 . set_xlabel ( 'time (s)' ) # Make the y-axis label and tick labels match the line color. ax1 . set_ylabel ( 'exp' , color = 'b' ) for tl in ax1 . get_yticklabels (): tl . set_color ( 'b' ) ax2 = ax1 . twinx () s2 = np . sin ( 2 * np . pi * t ) ax2 . plot ( t , s2 , 'r.' ) ax2 . set_ylabel ( 'sin' , color = 'r' ) for tl in ax2 . get_yticklabels (): tl . set_color ( 'r' ) plt . show () Keywords: python, matplotlib, pylab, example, codex (see Search examples ) © Copyright 2002 - 2012 John Hunter, Darren Dale, Eric Firing, Michael Droettboom and the matplotlib development team; 2012 - 2014 The matplotlib development team.
Last updated on Jun 18, 2016. Created using Sphinx 1.3.5.