Skip to content

Commit 26f8ccc

Browse files
authored
Merge pull request matplotlib#18913 from anntzer/host
Factor out common functionality in HostAxes.twin{,x,y}.
2 parents b561849 + de2415b commit 26f8ccc

File tree

1 file changed

+24
-40
lines changed

1 file changed

+24
-40
lines changed

Diff for: lib/mpl_toolkits/axes_grid1/parasite_axes.py

+24-40
Original file line numberDiff line numberDiff line change
@@ -248,21 +248,11 @@ def twinx(self, axes_class=None):
248248
The y-axis of self will have ticks on the left and the returned axes
249249
will have ticks on the right.
250250
"""
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)
260252
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
266256

267257
def twiny(self, axes_class=None):
268258
"""
@@ -271,21 +261,11 @@ def twiny(self, axes_class=None):
271261
The x-axis of self will have ticks on the bottom and the returned axes
272262
will have ticks on the top.
273263
"""
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)
283265
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
289269

290270
def twin(self, aux_trans=None, axes_class=None):
291271
"""
@@ -294,23 +274,27 @@ def twin(self, aux_trans=None, axes_class=None):
294274
While self will have ticks on the left and bottom axis, the returned
295275
axes will have ticks on the top and right axis.
296276
"""
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-
302277
if aux_trans is None:
303278
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")
308281
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
309285

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`.
312289
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
314298

315299
def _remove_any_twin(self, ax):
316300
self.parasites.remove(ax)

0 commit comments

Comments
 (0)