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

Commit86d5797

Browse files
authored
Merge pull request#23740 from QuLogic/parentless-mappable
Clarify error for colorbar with unparented mappable
2 parents942aa77 +5509ebb commit86d5797

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
ifaxisNone:
1248-
ax=getattr(mappable,"axes",self.gca())
1248+
ax=getattr(mappable,"axes",None)
12491249

12501250
if (self.get_layout_engine()isnotNoneand
12511251
notself.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
ifcaxisNone:
1255+
ifaxisNone:
1256+
raiseValueError(
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_gridspecandisinstance(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+
deftest_parentless_mappable():
319+
pc=mpl.collections.PatchCollection([],cmap=plt.get_cmap('viridis'))
320+
pc.set_array([])
321+
322+
withpytest.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
deftest_colorbar_closed_patch():
320328
# Remove this line when this test image is regenerated.
@@ -675,7 +683,7 @@ def test_colorbar_inverted_ticks():
675683
deftest_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)

[8]ページ先頭

©2009-2025 Movatter.jp