@@ -248,21 +248,11 @@ def twinx(self, axes_class=None):
248
248
The y-axis of self will have ticks on the left and the returned axes
249
249
will have ticks on the right.
250
250
"""
251
- if axes_class is None :
252
- axes_class = self ._get_base_axes ()
253
-
254
- parasite_axes_class = parasite_axes_class_factory (axes_class )
255
-
256
- ax2 = parasite_axes_class (self , sharex = self )
257
- self .parasites .append (ax2 )
258
- ax2 ._remove_method = self ._remove_any_twin
259
-
251
+ ax = self ._add_twin_axes (axes_class , sharex = self )
260
252
self .axis ["right" ].set_visible (False )
261
-
262
- ax2 .axis ["right" ].set_visible (True )
263
- ax2 .axis ["left" , "top" , "bottom" ].set_visible (False )
264
-
265
- return ax2
253
+ ax .axis ["right" ].set_visible (True )
254
+ ax .axis ["left" , "top" , "bottom" ].set_visible (False )
255
+ return ax
266
256
267
257
def twiny (self , axes_class = None ):
268
258
"""
@@ -271,21 +261,11 @@ def twiny(self, axes_class=None):
271
261
The x-axis of self will have ticks on the bottom and the returned axes
272
262
will have ticks on the top.
273
263
"""
274
- if axes_class is None :
275
- axes_class = self ._get_base_axes ()
276
-
277
- parasite_axes_class = parasite_axes_class_factory (axes_class )
278
-
279
- ax2 = parasite_axes_class (self , sharey = self )
280
- self .parasites .append (ax2 )
281
- ax2 ._remove_method = self ._remove_any_twin
282
-
264
+ ax = self ._add_twin_axes (axes_class , sharey = self )
283
265
self .axis ["top" ].set_visible (False )
284
-
285
- ax2 .axis ["top" ].set_visible (True )
286
- ax2 .axis ["left" , "right" , "bottom" ].set_visible (False )
287
-
288
- return ax2
266
+ ax .axis ["top" ].set_visible (True )
267
+ ax .axis ["left" , "right" , "bottom" ].set_visible (False )
268
+ return ax
289
269
290
270
def twin (self , aux_trans = None , axes_class = None ):
291
271
"""
@@ -294,23 +274,27 @@ def twin(self, aux_trans=None, axes_class=None):
294
274
While self will have ticks on the left and bottom axis, the returned
295
275
axes will have ticks on the top and right axis.
296
276
"""
297
- if axes_class is None :
298
- axes_class = self ._get_base_axes ()
299
-
300
- parasite_axes_class = parasite_axes_class_factory (axes_class )
301
-
302
277
if aux_trans is None :
303
278
aux_trans = mtransforms .IdentityTransform ()
304
- ax2 = parasite_axes_class (self , aux_trans , viewlim_mode = "transform" )
305
- self .parasites .append (ax2 )
306
- ax2 ._remove_method = self ._remove_any_twin
307
-
279
+ ax = self ._add_twin_axes (
280
+ axes_class , aux_transform = aux_trans , viewlim_mode = "transform" )
308
281
self .axis ["top" , "right" ].set_visible (False )
282
+ ax .axis ["top" , "right" ].set_visible (True )
283
+ ax .axis ["left" , "bottom" ].set_visible (False )
284
+ return ax
309
285
310
- ax2 .axis ["top" , "right" ].set_visible (True )
311
- ax2 .axis ["left" , "bottom" ].set_visible (False )
286
+ def _add_twin_axes (self , axes_class , ** kwargs ):
287
+ """
288
+ Helper for `.twinx`/`.twiny`/`.twin`.
312
289
313
- return ax2
290
+ *kwargs* are forwarded to the parasite axes constructor.
291
+ """
292
+ if axes_class is None :
293
+ axes_class = self ._get_base_axes ()
294
+ ax = parasite_axes_class_factory (axes_class )(self , ** kwargs )
295
+ self .parasites .append (ax )
296
+ ax ._remove_method = self ._remove_any_twin
297
+ return ax
314
298
315
299
def _remove_any_twin (self , ax ):
316
300
self .parasites .remove (ax )
0 commit comments