Skip to content

Move label hiding rectilinear-only check into _label_outer_{x,y}axis. #21317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions lib/matplotlib/axes/_subplots.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import matplotlib as mpl
from matplotlib import _api, cbook
from matplotlib.axes._axes import Axes
from matplotlib.gridspec import GridSpec, SubplotSpec
Expand Down Expand Up @@ -109,10 +110,13 @@ def label_outer(self):
labels are on the top side); y-labels only for subplots on the first
column (or last column, if labels are on the right side).
"""
self._label_outer_xaxis()
self._label_outer_yaxis()
self._label_outer_xaxis(check_patch=False)
self._label_outer_yaxis(check_patch=False)

def _label_outer_xaxis(self):
def _label_outer_xaxis(self, *, check_patch):
# see documentation in label_outer.
if check_patch and not isinstance(self.patch, mpl.patches.Rectangle):
return
ss = self.get_subplotspec()
label_position = self.xaxis.get_label_position()
if not ss.is_first_row(): # Remove top label/ticklabels/offsettext.
Expand All @@ -128,7 +132,10 @@ def _label_outer_xaxis(self):
if self.xaxis.offsetText.get_position()[1] == 0:
self.xaxis.offsetText.set_visible(False)

def _label_outer_yaxis(self):
def _label_outer_yaxis(self, *, check_patch):
# see documentation in label_outer.
if check_patch and not isinstance(self.patch, mpl.patches.Rectangle):
return
ss = self.get_subplotspec()
label_position = self.yaxis.get_label_position()
if not ss.is_first_col(): # Remove left label/ticklabels/offsettext.
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1910,10 +1910,10 @@ def _do_layout(gs, mosaic, unique_ids, nested):
for ax in ret.values():
if sharex:
ax.sharex(ax0)
ax._label_outer_xaxis()
ax._label_outer_xaxis(check_patch=True)
if sharey:
ax.sharey(ax0)
ax._label_outer_yaxis()
ax._label_outer_yaxis(check_patch=True)
for k, ax in ret.items():
if isinstance(k, str):
ax.set_label(k)
Expand Down
13 changes: 6 additions & 7 deletions lib/matplotlib/gridspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,12 @@ def subplots(self, *, sharex=False, sharey=False, squeeze=True,
self[row, col], **subplot_kw)

# turn off redundant tick labeling
if all(ax.name == "rectilinear" for ax in axarr.flat):
if sharex in ["col", "all"]:
for ax in axarr.flat:
ax._label_outer_xaxis()
if sharey in ["row", "all"]:
for ax in axarr.flat:
ax._label_outer_yaxis()
if sharex in ["col", "all"]:
for ax in axarr.flat:
ax._label_outer_xaxis(check_patch=True)
if sharey in ["row", "all"]:
for ax in axarr.flat:
ax._label_outer_yaxis(check_patch=True)

if squeeze:
# Discarding unneeded dimensions that equal 1. If we only have one
Expand Down
7 changes: 6 additions & 1 deletion lib/matplotlib/tests/test_polar.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,15 @@ def test_remove_shared_polar(fig_ref, fig_test):

def test_shared_polar_keeps_ticklabels():
fig, axs = plt.subplots(
2, 2, subplot_kw=dict(projection="polar"), sharex=True, sharey=True)
2, 2, subplot_kw={"projection": "polar"}, sharex=True, sharey=True)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just to make things fit in 79 characters in the equivalent expression below.

fig.canvas.draw()
assert axs[0, 1].xaxis.majorTicks[0].get_visible()
assert axs[0, 1].yaxis.majorTicks[0].get_visible()
fig, axs = plt.subplot_mosaic(
"ab\ncd", subplot_kw={"projection": "polar"}, sharex=True, sharey=True)
fig.canvas.draw()
assert axs["b"].xaxis.majorTicks[0].get_visible()
assert axs["b"].yaxis.majorTicks[0].get_visible()


def test_axvline_axvspan_do_not_modify_rlims():
Expand Down