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

Skip to content

Commit 7c4eaea

Browse files
committed
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
1 parent f8df71b commit 7c4eaea

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lib/matplotlib/figure.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,14 +1173,14 @@ def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
11731173
if sharex in ["col", "all"]:
11741174
# turn off all but the bottom row
11751175
for ax in axarr[:-1, :].flat:
1176-
for label in ax.get_xticklabels():
1177-
label.set_visible(False)
1176+
ax.xaxis.set_tick_params(which='both', label1On=False, label2On=False)
11781177
ax.xaxis.offsetText.set_visible(False)
11791178
if sharey in ["row", "all"]:
11801179
# turn off all but the first column
11811180
for ax in axarr[:, 1:].flat:
1182-
for label in ax.get_yticklabels():
1181+
for label in ax.get_yticklabels(which='both'):
11831182
label.set_visible(False)
1183+
ax.yaxis.set_tick_params(which='both', label1On=False, label2On=False)
11841184
ax.yaxis.offsetText.set_visible(False)
11851185

11861186
if squeeze:

lib/matplotlib/tests/test_figure.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,24 @@ def test_invalid_figure_size():
298298

299299
with pytest.raises(ValueError):
300300
fig.add_axes((.1, .1, .5, np.nan))
301+
302+
303+
def test_subplots_shareax_loglabels():
304+
fig, ax_arr = plt.subplots(2, 2, sharex=True, sharey=True, squeeze=False)
305+
for ax in ax_arr.flatten():
306+
ax.plot([10, 20, 30], [10, 20, 30])
307+
308+
ax.set_yscale("log")
309+
ax.set_xscale("log")
310+
311+
for ax in ax_arr[0, :]:
312+
assert 0 == len(ax.xaxis.get_ticklabels(which='both'))
313+
314+
for ax in ax_arr[1, :]:
315+
assert 0 < len(ax.xaxis.get_ticklabels(which='both'))
316+
317+
for ax in ax_arr[:, 1]:
318+
assert 0 == len(ax.yaxis.get_ticklabels(which='both'))
319+
320+
for ax in ax_arr[:, 0]:
321+
assert 0 < len(ax.yaxis.get_ticklabels(which='both'))

0 commit comments

Comments
 (0)