Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix label_outer in the presence of colorbars.#30098

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
anntzer wants to merge1 commit intomatplotlib:main
base:main
Choose a base branch
Loading
fromanntzer:locb
Open
Show file tree
Hide file tree
Changes fromall 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
33 changes: 29 additions & 4 deletionslib/matplotlib/axes/_base.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4749,14 +4749,39 @@ def label_outer(self, remove_inner_ticks=False):
self._label_outer_yaxis(skip_non_rectangular_axes=False,
remove_inner_ticks=remove_inner_ticks)

def _get_subplotspec_with_optional_colorbar(self):
"""
Return the subplotspec for this Axes, except that if this Axes has been
moved to a subgridspec to make room for a colorbar, then return the
subplotspec that encloses both this Axes and the colorbar Axes.
"""
ss = self.get_subplotspec()
if not ss:
return
gs = ss.get_gridspec()
# Match against subgridspec hierarchy set up by colorbar.make_axes_gridspec.
if (isinstance(gs, mpl.gridspec.GridSpecFromSubplotSpec)
and gs.nrows * gs.ncols == 6):
Copy link
Member

Choose a reason for hiding this comment

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

Please add a comment what this magic is about.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

done

Copy link
Member

@timhoffmtimhoffmMay 23, 2025
edited
Loading

Choose a reason for hiding this comment

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

Thanks. Basically ok.

Would it make more sense thatmake_axes_gridspec explicitly flags the gridspec? Not sure what the right level of explicitness / specialization is, but it feels awkward to detect a gridspec for the colorbar thrugh nrows * ncols. Options could be (from less to more formal):

  • monkey-patch_is_colorbar_gridspec onto the gridspec, so that we can testif hasattr(gs, "_is_colorbar_gridspec")
  • add a_is_colorbar_gridspec attribute to all gridspecs, so that we can testif gs._is_colorbar_gridspec
  • Create aColorbarGridSpec subclass so that we can testif isinstance(gs, ColorbarGridSpec)

Copy link
Member

Choose a reason for hiding this comment

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

I would lean towards the least formal here - the subgridspec trick of making colorbars is pretty clunky, and note that it is explicitlynot used bylayout='constrained'.

The original ideas behind grid specs and subgridspecs were very good, but unfortunately loses the relationship between sibling axes if you change add the colorbar and make the colorbar and main axes child subgridspecs.

for ax in self.figure.axes:
if (ax is not self
and hasattr(ax, "_colorbar_info")
and ax.get_subplotspec()
and isinstance(ax.get_subplotspec().get_gridspec(),
mpl.gridspec.GridSpecFromSubplotSpec)
and (ax.get_subplotspec().get_gridspec()._subplot_spec
is gs._subplot_spec)):
ss = gs._subplot_spec
break
return ss

def _label_outer_xaxis(self, *, skip_non_rectangular_axes,
remove_inner_ticks=False):
# see documentation in label_outer.
if skip_non_rectangular_axes and not isinstance(self.patch,
mpl.patches.Rectangle):
return
ss = self.get_subplotspec()
ifnot ss:
ss = self._get_subplotspec_with_optional_colorbar()
ifss is None:
return
label_position = self.xaxis.get_label_position()
if not ss.is_first_row(): # Remove top label/ticklabels/offsettext.
Expand All@@ -4782,8 +4807,8 @@ def _label_outer_yaxis(self, *, skip_non_rectangular_axes,
if skip_non_rectangular_axes and not isinstance(self.patch,
mpl.patches.Rectangle):
return
ss = self.get_subplotspec()
ifnot ss:
ss = self._get_subplotspec_with_optional_colorbar()
ifss is None:
return
label_position = self.yaxis.get_label_position()
if not ss.is_first_col(): # Remove left label/ticklabels/offsettext.
Expand Down
10 changes: 8 additions & 2 deletionslib/matplotlib/tests/test_subplots.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,6 +4,7 @@
import numpy as np
import pytest

import matplotlib as mpl
from matplotlib.axes import Axes, SubplotBase
import matplotlib.pyplot as plt
from matplotlib.testing.decorators import check_figures_equal, image_comparison
Expand DownExpand Up@@ -111,10 +112,15 @@ def test_shared():


@pytest.mark.parametrize('remove_ticks', [True, False])
def test_label_outer(remove_ticks):
f, axs = plt.subplots(2, 2, sharex=True, sharey=True)
@pytest.mark.parametrize('layout_engine', ['tight', 'constrained'])
@pytest.mark.parametrize('with_colorbar', [True, False])
def test_label_outer(remove_ticks, layout_engine, with_colorbar):
fig = plt.figure(layout=layout_engine)
axs = fig.subplots(2, 2, sharex=True, sharey=True)
for ax in axs.flat:
ax.set(xlabel="foo", ylabel="bar")
if with_colorbar:
fig.colorbar(mpl.cm.ScalarMappable(), ax=ax)
ax.label_outer(remove_inner_ticks=remove_ticks)
check_ticklabel_visible(
axs.flat, [False, False, True, True], [True, False, True, False])
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp