Skip to content

Clarify error for colorbar with unparented mappable #23740

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
Aug 26, 2022
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
5 changes: 4 additions & 1 deletion doc/api/next_api_changes/removals/22081-AL.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
colorbar defaults to stealing space from the mappable's axes rather than the current axes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Pass ``ax=plt.gca()`` to restore the previous behavior.
If the mappable does not have an Axes, then an error will be raised.

Pass the *cax* or *ax* argument to be explicit about where the colorbar will be
placed. Passing ``ax=plt.gca()`` will restore the previous behavior.

Removal of deprecated ``colorbar`` APIs
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
8 changes: 7 additions & 1 deletion lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1245,13 +1245,19 @@ def colorbar(
"""

if ax is None:
ax = getattr(mappable, "axes", self.gca())
ax = getattr(mappable, "axes", None)

if (self.get_layout_engine() is not None and
not self.get_layout_engine().colorbar_gridspec):
use_gridspec = False
# Store the value of gca so that we can set it back later on.
if cax is None:
if ax is None:
raise ValueError(
'Unable to determine Axes to steal space for Colorbar. '
'Either provide the *cax* argument to use as the Axes for '
'the Colorbar, provide the *ax* argument to steal space '
'from it, or add *mappable* to an Axes.')
current_ax = self.gca()
userax = False
if (use_gridspec and isinstance(ax, SubplotBase)):
Expand Down
10 changes: 9 additions & 1 deletion lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,14 @@ def test_colorbarbase():
Colorbar(ax, cmap=plt.cm.bone)


def test_parentless_mappable():
pc = mpl.collections.PatchCollection([], cmap=plt.get_cmap('viridis'))
pc.set_array([])

with pytest.raises(ValueError, match='Unable to determine Axes to steal'):
plt.colorbar(pc)


@image_comparison(['colorbar_closed_patch.png'], remove_text=True)
def test_colorbar_closed_patch():
# Remove this line when this test image is regenerated.
Expand Down Expand Up @@ -675,7 +683,7 @@ def test_colorbar_inverted_ticks():
def test_mappable_no_alpha():
fig, ax = plt.subplots()
sm = cm.ScalarMappable(norm=mcolors.Normalize(), cmap='viridis')
fig.colorbar(sm)
fig.colorbar(sm, ax=ax)
sm.set_cmap('plasma')
plt.draw()

Expand Down