3
3
Secondary Axis
4
4
==============
5
5
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
7
7
radians to degrees on the same plot. We can do this by making a child
8
8
axes with only one axis visible via `.Axes.axes.secondary_xaxis` and
9
9
`.Axes.axes.secondary_yaxis`. This secondary axis can have a different scale
@@ -35,6 +35,7 @@ def deg2rad(x):
35
35
def rad2deg (x ):
36
36
return x * 180 / np .pi
37
37
38
+
38
39
secax = ax .secondary_xaxis ('top' , functions = (deg2rad , rad2deg ))
39
40
secax .set_xlabel ('angle [rad]' )
40
41
plt .show ()
@@ -65,6 +66,7 @@ def forward(x):
65
66
def inverse (x ):
66
67
return 1 / x
67
68
69
+
68
70
secax = ax .secondary_xaxis ('top' , functions = (forward , inverse ))
69
71
secax .set_xlabel ('period [s]' )
70
72
plt .show ()
@@ -97,6 +99,7 @@ def forward(x):
97
99
def inverse (x ):
98
100
return np .interp (x , xnew , xold )
99
101
102
+
100
103
secax = ax .secondary_xaxis ('top' , functions = (forward , inverse ))
101
104
secax .xaxis .set_minor_locator (AutoMinorLocator ())
102
105
secax .set_xlabel ('$X_{other}$' )
@@ -109,7 +112,7 @@ def inverse(x):
109
112
110
113
111
114
dates = [datetime .datetime (2018 , 1 , 1 ) + datetime .timedelta (hours = k * 6 )
112
- for k in range (240 )]
115
+ for k in range (240 )]
113
116
temperature = np .random .randn (len (dates ))
114
117
fig , ax = plt .subplots (constrained_layout = True )
115
118
@@ -129,6 +132,7 @@ def yday2date(x):
129
132
y = x + mdates .date2num (datetime .datetime (2018 , 1 , 1 ))
130
133
return y
131
134
135
+
132
136
secaxx = ax .secondary_xaxis ('top' , functions = (date2yday , yday2date ))
133
137
secaxx .set_xlabel ('yday [2018]' )
134
138
@@ -140,6 +144,7 @@ def CtoF(x):
140
144
def FtoC (x ):
141
145
return (x - 32 ) / 1.8
142
146
147
+
143
148
secaxy = ax .secondary_yaxis ('right' , functions = (CtoF , FtoC ))
144
149
secaxy .set_ylabel (r'$T\ [^oF]$' )
145
150
0 commit comments