File tree 3 files changed +20
-3
lines changed
doc/api/next_api_changes/removals
3 files changed +20
-3
lines changed Original file line number Diff line number Diff line change 1
1
colorbar defaults to stealing space from the mappable's axes rather than the current axes
2
2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3
3
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.
5
8
6
9
Removal of deprecated ``colorbar `` APIs
7
10
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Original file line number Diff line number Diff line change @@ -1245,13 +1245,19 @@ def colorbar(
1245
1245
"""
1246
1246
1247
1247
if ax is None :
1248
- ax = getattr (mappable , "axes" , self . gca () )
1248
+ ax = getattr (mappable , "axes" , None )
1249
1249
1250
1250
if (self .get_layout_engine () is not None and
1251
1251
not self .get_layout_engine ().colorbar_gridspec ):
1252
1252
use_gridspec = False
1253
1253
# Store the value of gca so that we can set it back later on.
1254
1254
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.' )
1255
1261
current_ax = self .gca ()
1256
1262
userax = False
1257
1263
if (use_gridspec and isinstance (ax , SubplotBase )):
Original file line number Diff line number Diff line change @@ -315,6 +315,14 @@ def test_colorbarbase():
315
315
Colorbar (ax , cmap = plt .cm .bone )
316
316
317
317
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
+
318
326
@image_comparison (['colorbar_closed_patch.png' ], remove_text = True )
319
327
def test_colorbar_closed_patch ():
320
328
# Remove this line when this test image is regenerated.
@@ -675,7 +683,7 @@ def test_colorbar_inverted_ticks():
675
683
def test_mappable_no_alpha ():
676
684
fig , ax = plt .subplots ()
677
685
sm = cm .ScalarMappable (norm = mcolors .Normalize (), cmap = 'viridis' )
678
- fig .colorbar (sm )
686
+ fig .colorbar (sm , ax = ax )
679
687
sm .set_cmap ('plasma' )
680
688
plt .draw ()
681
689
You can’t perform that action at this time.
0 commit comments