Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e7e8bcb

Browse files
committed
Merge pull request #4898 from productivememberofsociety666/add_remove_to_axes_grid1_twin_axes
HostAxesBase now adds appropriate _remove_method to its parasite axes.
2 parents 333136b + d268abf commit e7e8bcb

File tree

4 files changed

+64
-49
lines changed

4 files changed

+64
-49
lines changed

doc/users/whats_new.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,13 @@ way as setting `CC` or `CXX` before building. An example follows.
742742

743743
export PKG_CONFIG=x86_64-pc-linux-gnu-pkg-config
744744

745+
AxesGrid toolkit
746+
----------------
747+
748+
Twins of host axes (see `~mpl_toolkits.axes_grid1.host_subplot()` and its
749+
``twin*`` methods) now have ``remove()`` functionality. Some visibility changes
750+
made by the ``twin*`` methods were modified to faciliate this feature.
751+
745752
.. _whats-new-1-4:
746753

747754
new in matplotlib-1.4

lib/mpl_toolkits/axes_grid1/parasite_axes.py

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ def get_aux_axes(self, tr, viewlim_mode="equal", axes_class=None):
255255
# note that ax2.transData == tr + ax1.transData
256256
# Anthing you draw in ax2 will match the ticks and grids of ax1.
257257
self.parasites.append(ax2)
258+
ax2._remove_method = lambda h: self.parasites.remove(h)
258259
return ax2
259260

260261

@@ -326,17 +327,16 @@ def twinx(self, axes_class=None):
326327
ax2 = parasite_axes_class(self, sharex=self, frameon=False)
327328
self.parasites.append(ax2)
328329

329-
# for normal axes
330-
331-
self.axis["right"].toggle(all=False)
332-
self.axis["right"].line.set_visible(True)
330+
self.axis["right"].set_visible(False)
333331

334332
ax2.axis["right"].set_visible(True)
335-
ax2.axis["left","top", "bottom"].toggle(all=False)
336-
ax2.axis["left","top", "bottom"].line.set_visible(False)
333+
ax2.axis["left", "top", "bottom"].set_visible(False)
337334

338-
ax2.axis["right"].toggle(all=True)
339-
ax2.axis["right"].line.set_visible(False)
335+
def _remove_method(h):
336+
self.parasites.remove(h)
337+
self.axis["right"].set_visible(True)
338+
self.axis["right"].toggle(ticklabels=False, label=False)
339+
ax2._remove_method = _remove_method
340340

341341
return ax2
342342

@@ -360,15 +360,16 @@ def twiny(self, axes_class=None):
360360
ax2 = parasite_axes_class(self, sharey=self, frameon=False)
361361
self.parasites.append(ax2)
362362

363-
self.axis["top"].toggle(all=False)
364-
self.axis["top"].line.set_visible(True)
363+
self.axis["top"].set_visible(False)
365364

366365
ax2.axis["top"].set_visible(True)
367-
ax2.axis["left","right", "bottom"].toggle(all=False)
368-
ax2.axis["left","right", "bottom"].line.set_visible(False)
366+
ax2.axis["left", "right", "bottom"].set_visible(False)
369367

370-
ax2.axis["top"].toggle(all=True)
371-
ax2.axis["top"].line.set_visible(False)
368+
def _remove_method(h):
369+
self.parasites.remove(h)
370+
self.axis["top"].set_visible(True)
371+
self.axis["top"].toggle(ticklabels=False, label=False)
372+
ax2._remove_method = _remove_method
372373

373374
return ax2
374375

@@ -399,43 +400,18 @@ def twin(self, aux_trans=None, axes_class=None):
399400
viewlim_mode="transform",
400401
)
401402
self.parasites.append(ax2)
403+
ax2._remove_method = lambda h: self.parasites.remove(h)
402404

405+
self.axis["top", "right"].set_visible(False)
403406

404-
# for normal axes
405-
#self.yaxis.tick_left()
406-
#self.xaxis.tick_bottom()
407-
#ax2.yaxis.tick_right()
408-
#ax2.xaxis.set_visible(True)
409-
#ax2.yaxis.set_visible(True)
410-
411-
#ax2.yaxis.set_label_position('right')
412-
##ax2.xaxis.tick_top()
413-
#ax2.xaxis.set_label_position('top')
414-
415-
416-
self.axis["top","right"].toggle(all=False)
417-
self.axis["top","right"].line.set_visible(False)
418-
#self.axis["left","bottom"].toggle(label=True)
419-
420-
ax2.axis["top","right"].set_visible(True)
421-
422-
ax2.axis["bottom","left"].toggle(all=False)
423-
ax2.axis["bottom","left"].line.set_visible(False)
424-
425-
ax2.axis["top","right"].toggle(all=True)
426-
ax2.axis["top","right"].line.set_visible(True)
427-
428-
429-
# # for axisline axes
430-
# self._axislines["right"].set_visible(False)
431-
# self._axislines["top"].set_visible(False)
432-
# ax2._axislines["left"].set_visible(False)
433-
# ax2._axislines["bottom"].set_visible(False)
407+
ax2.axis["top", "right"].set_visible(True)
408+
ax2.axis["left", "bottom"].set_visible(False)
434409

435-
# ax2._axislines["right"].set_visible(True)
436-
# ax2._axislines["top"].set_visible(True)
437-
# ax2._axislines["right"].major_ticklabels.set_visible(True)
438-
# ax2._axislines["top"].major_ticklabels.set_visible(True)
410+
def _remove_method(h):
411+
self.parasites.remove(h)
412+
self.axis["top", "right"].set_visible(True)
413+
self.axis["top", "right"].toggle(ticklabels=False, label=False)
414+
ax2._remove_method = _remove_method
439415

440416
return ax2
441417

lib/mpl_toolkits/tests/test_axes_grid1.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
from matplotlib.externals import six
55

6+
import matplotlib
67
import matplotlib.pyplot as plt
78
from matplotlib.testing.decorators import image_comparison
8-
from mpl_toolkits.axes_grid1 import make_axes_locatable
9+
from mpl_toolkits.axes_grid1 import make_axes_locatable, host_subplot
10+
from itertools import product
911
import numpy as np
1012

1113

@@ -50,6 +52,36 @@ def test_divider_append_axes():
5052
axHistleft.yaxis.set_ticklabels(())
5153
axHistright.yaxis.set_ticklabels(())
5254

55+
56+
@image_comparison(baseline_images=['twin_axes_empty_and_removed'],
57+
extensions=["png"])
58+
def test_twin_axes_empty_and_removed():
59+
# Purely cosmetic font changes (avoid overlap)
60+
matplotlib.rcParams.update({"font.size": 8})
61+
matplotlib.rcParams.update({"xtick.labelsize": 8})
62+
matplotlib.rcParams.update({"ytick.labelsize": 8})
63+
generators = [ "twinx", "twiny", "twin" ]
64+
modifiers = [ "", "host invisible", "twin removed", "twin invisible",
65+
"twin removed\nhost invisible" ]
66+
# Unmodified host subplot at the beginning for reference
67+
h = host_subplot(len(modifiers)+1, len(generators), 2)
68+
h.text(0.5, 0.5, "host_subplot", horizontalalignment="center",
69+
verticalalignment="center")
70+
# Host subplots with various modifications (twin*, visibility) applied
71+
for i, (mod, gen) in enumerate(product(modifiers, generators),
72+
len(generators)+1):
73+
h = host_subplot(len(modifiers)+1, len(generators), i)
74+
t = getattr(h, gen)()
75+
if "twin invisible" in mod:
76+
t.axis[:].set_visible(False)
77+
if "twin removed" in mod:
78+
t.remove()
79+
if "host invisible" in mod:
80+
h.axis[:].set_visible(False)
81+
h.text(0.5, 0.5, gen + ("\n" + mod if mod else ""),
82+
horizontalalignment="center", verticalalignment="center")
83+
plt.subplots_adjust(wspace=0.5, hspace=1)
84+
5385
if __name__ == '__main__':
5486
import nose
5587
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)