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

Skip to content

FIX: manual colorbars and tight layout #21751

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 28, 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
2 changes: 2 additions & 0 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ def get_subplotspec(self):
# make tight_layout happy..
ss = getattr(self._cbar.ax, 'get_subplotspec', None)
if ss is None:
if self._orig_locator is None:
return None
ss = self._orig_locator.get_subplotspec()
else:
ss = ss()
Expand Down
13 changes: 13 additions & 0 deletions lib/matplotlib/tests/test_tightlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,16 @@ def __init__(self, *args, **kwargs):
monkeypatch.setattr(mpl.backend_bases.RendererBase, "__init__", __init__)
fig, ax = plt.subplots()
fig.tight_layout()


def test_manual_colorbar():
# This should warn, but not raise
fig, axes = plt.subplots(1, 2)
pts = axes[1].scatter([0, 1], [0, 1], c=[1, 5])
ax_rect = axes[1].get_position()
cax = fig.add_axes(
[ax_rect.x1 + 0.005, ax_rect.y0, 0.015, ax_rect.height]
)
fig.colorbar(pts, cax=cax)
with pytest.warns(UserWarning, match="This figure includes Axes"):
fig.tight_layout()
13 changes: 7 additions & 6 deletions lib/matplotlib/tight_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,14 @@ def get_subplotspec_list(axes_list, grid_spec=None):

if hasattr(axes_or_locator, "get_subplotspec"):
subplotspec = axes_or_locator.get_subplotspec()
subplotspec = subplotspec.get_topmost_subplotspec()
gs = subplotspec.get_gridspec()
if grid_spec is not None:
if gs != grid_spec:
if subplotspec is not None:
subplotspec = subplotspec.get_topmost_subplotspec()
gs = subplotspec.get_gridspec()
if grid_spec is not None:
if gs != grid_spec:
subplotspec = None
elif gs.locally_modified_subplot_params():
subplotspec = None
elif gs.locally_modified_subplot_params():
subplotspec = None
else:
subplotspec = None

Expand Down