33Secondary Axis
44==============
55
6- Sometimes we want as secondary axis on a plot, for instance to convert
6+ Sometimes we want a secondary axis on a plot, for instance to convert
77radians to degrees on the same plot. We can do this by making a child
88axes with only one axis visible via `.Axes.axes.secondary_xaxis` and
99`.Axes.axes.secondary_yaxis`. This secondary axis can have a different scale
@@ -35,6 +35,7 @@ def deg2rad(x):
3535def rad2deg (x ):
3636 return x * 180 / np .pi
3737
38+
3839secax = ax .secondary_xaxis ('top' , functions = (deg2rad , rad2deg ))
3940secax .set_xlabel ('angle [rad]' )
4041plt .show ()
@@ -65,6 +66,7 @@ def forward(x):
6566def inverse (x ):
6667 return 1 / x
6768
69+
6870secax = ax .secondary_xaxis ('top' , functions = (forward , inverse ))
6971secax .set_xlabel ('period [s]' )
7072plt .show ()
@@ -97,6 +99,7 @@ def forward(x):
9799def inverse (x ):
98100 return np .interp (x , xnew , xold )
99101
102+
100103secax = ax .secondary_xaxis ('top' , functions = (forward , inverse ))
101104secax .xaxis .set_minor_locator (AutoMinorLocator ())
102105secax .set_xlabel ('$X_{other}$' )
@@ -109,7 +112,7 @@ def inverse(x):
109112
110113
111114dates = [datetime .datetime (2018 , 1 , 1 ) + datetime .timedelta (hours = k * 6 )
112- for k in range (240 )]
115+ for k in range (240 )]
113116temperature = np .random .randn (len (dates ))
114117fig , ax = plt .subplots (constrained_layout = True )
115118
@@ -129,6 +132,7 @@ def yday2date(x):
129132 y = x + mdates .date2num (datetime .datetime (2018 , 1 , 1 ))
130133 return y
131134
135+
132136secaxx = ax .secondary_xaxis ('top' , functions = (date2yday , yday2date ))
133137secaxx .set_xlabel ('yday [2018]' )
134138
@@ -140,6 +144,7 @@ def CtoF(x):
140144def FtoC (x ):
141145 return (x - 32 ) / 1.8
142146
147+
143148secaxy = ax .secondary_yaxis ('right' , functions = (CtoF , FtoC ))
144149secaxy .set_ylabel (r'$T\ [^oF]$' )
145150
0 commit comments