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
This repository was archived by the owner on Nov 9, 2022. It is now read-only.

Commit 86d5797

Browse files
Merge pull request matplotlib#23740 from QuLogic/parentless-mappable
Clarify error for colorbar with unparented mappable
2 parents 942aa77 + 5509ebb commit 86d5797

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

‎doc/api/next_api_changes/removals/22081-AL.rst‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
colorbar defaults to stealing space from the mappable's axes rather than the current axes
22
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33

4-
Pass ``ax=plt.gca()`` to restore the previous behavior.
4+
If the mappable does not have an Axes, then an error will be raised.
5+
6+
Pass the *cax* or *ax* argument to be explicit about where the colorbar will be
7+
placed. Passing ``ax=plt.gca()`` will restore the previous behavior.
58

69
Removal of deprecated ``colorbar`` APIs
710
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

‎lib/matplotlib/figure.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1245,13 +1245,19 @@ def colorbar(
12451245
"""
12461246

12471247
if ax is None:
1248-
ax = getattr(mappable, "axes", self.gca())
1248+
ax = getattr(mappable, "axes", None)
12491249

12501250
if (self.get_layout_engine() is not None and
12511251
not self.get_layout_engine().colorbar_gridspec):
12521252
use_gridspec = False
12531253
# Store the value of gca so that we can set it back later on.
12541254
if cax is None:
1255+
if ax is None:
1256+
raise ValueError(
1257+
'Unable to determine Axes to steal space for Colorbar. '
1258+
'Either provide the *cax* argument to use as the Axes for '
1259+
'the Colorbar, provide the *ax* argument to steal space '
1260+
'from it, or add *mappable* to an Axes.')
12551261
current_ax = self.gca()
12561262
userax = False
12571263
if (use_gridspec and isinstance(ax, SubplotBase)):

‎lib/matplotlib/tests/test_colorbar.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,14 @@ def test_colorbarbase():
315315
Colorbar(ax, cmap=plt.cm.bone)
316316

317317

318+
def test_parentless_mappable():
319+
pc = mpl.collections.PatchCollection([], cmap=plt.get_cmap('viridis'))
320+
pc.set_array([])
321+
322+
with pytest.raises(ValueError, match='Unable to determine Axes to steal'):
323+
plt.colorbar(pc)
324+
325+
318326
@image_comparison(['colorbar_closed_patch.png'], remove_text=True)
319327
def test_colorbar_closed_patch():
320328
# Remove this line when this test image is regenerated.
@@ -675,7 +683,7 @@ def test_colorbar_inverted_ticks():
675683
def test_mappable_no_alpha():
676684
fig, ax = plt.subplots()
677685
sm = cm.ScalarMappable(norm=mcolors.Normalize(), cmap='viridis')
678-
fig.colorbar(sm)
686+
fig.colorbar(sm, ax=ax)
679687
sm.set_cmap('plasma')
680688
plt.draw()
681689

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /