From 8fd417e93a332ae2bd7c83bd861f1a9dbbcd56bd Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Sun, 6 Aug 2017 17:50:40 -0400 Subject: [PATCH] FIX: logscale + subplots share axes Use `set_tick_params` to hide tick labels in not-edge plots instead of setting the visibility on the tick label objects. This catches both major and minor tick-labels and is more robust to changes in the ticklabel generation. closes #8903 --- lib/matplotlib/figure.py | 8 +++++--- lib/matplotlib/tests/test_figure.py | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 01c1a1329c4a..d78582726927 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1173,14 +1173,16 @@ def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False, if sharex in ["col", "all"]: # turn off all but the bottom row for ax in axarr[:-1, :].flat: - for label in ax.get_xticklabels(): - label.set_visible(False) + ax.xaxis.set_tick_params(which='both', + label1On=False, label2On=False) ax.xaxis.offsetText.set_visible(False) if sharey in ["row", "all"]: # turn off all but the first column for ax in axarr[:, 1:].flat: - for label in ax.get_yticklabels(): + for label in ax.get_yticklabels(which='both'): label.set_visible(False) + ax.yaxis.set_tick_params(which='both', + label1On=False, label2On=False) ax.yaxis.offsetText.set_visible(False) if squeeze: diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index d2c8b7aef68d..5f2d3b1b2a82 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -298,3 +298,24 @@ def test_invalid_figure_size(): with pytest.raises(ValueError): fig.add_axes((.1, .1, .5, np.nan)) + + +def test_subplots_shareax_loglabels(): + fig, ax_arr = plt.subplots(2, 2, sharex=True, sharey=True, squeeze=False) + for ax in ax_arr.flatten(): + ax.plot([10, 20, 30], [10, 20, 30]) + + ax.set_yscale("log") + ax.set_xscale("log") + + for ax in ax_arr[0, :]: + assert 0 == len(ax.xaxis.get_ticklabels(which='both')) + + for ax in ax_arr[1, :]: + assert 0 < len(ax.xaxis.get_ticklabels(which='both')) + + for ax in ax_arr[:, 1]: + assert 0 == len(ax.yaxis.get_ticklabels(which='both')) + + for ax in ax_arr[:, 0]: + assert 0 < len(ax.yaxis.get_ticklabels(which='both'))