@@ -235,24 +235,43 @@ class ColorbarAxes(Axes):
235
235
Users should not normally instantiate this class, but it is the class
236
236
returned by ``cbar = fig.colorbar(im); cax = cbar.ax``.
237
237
"""
238
- def __init__ (self , parent ):
238
+ def __init__ (self , parent , userax = False ):
239
239
"""
240
+ Parameters
241
+ ----------
242
+ parent : Axes
243
+ Axes that specifies the position of the colorbar.
244
+ userax : boolean
245
+ True if the user passed `.Figure.colorbar` the axes manually.
240
246
"""
241
- inner_ax = parent .inset_axes ([0 , 0 , 1 , 1 ])
242
- #self.__class__ = type(inner_ax.__class__.__name__+"ColorBar",
243
- # (self.__class__, inner_ax.__class__),
244
- # {})
247
+
248
+ if userax :
249
+ # copy position:
250
+ fig = parent .figure
251
+ parent_ax = fig .add_axes (parent .get_position ())
252
+ # copy the locator if one exists:
253
+ parent_ax ._axes_locator = parent ._axes_locator
254
+ # remove the parent from the figure...
255
+ parent .remove ()
256
+ else :
257
+ parent_ax = parent
258
+
259
+ inner_ax = parent_ax .inset_axes ([0 , 0 , 1 , 1 ])
245
260
self .__dict__ = inner_ax .__dict__
246
- self .parent_ax = parent
261
+
262
+ self .parent_ax = parent_ax
247
263
self .inner_ax = inner_ax
248
264
self .parent_ax .xaxis .set_visible (False )
249
265
self .parent_ax .yaxis .set_visible (False )
250
266
self .parent_ax .set_facecolor ('none' )
251
- # map some features to the parent so users have access...
252
267
self .parent_ax .tick_params = self .inner_ax .tick_params
253
-
268
+ self .parent_ax .set_xticks = self .inner_ax .set_xticks
269
+ self .parent_ax .set_yticks = self .inner_ax .set_yticks
254
270
for attr in ["get_position" , "set_position" , "set_aspect" ]:
255
271
setattr (self , attr , getattr (self .parent_ax , attr ))
272
+ if userax :
273
+ # point the parent's methods all at this axes...
274
+ parent .__dict__ = self .__dict__
256
275
257
276
def _set_inner_bounds (self , bounds ):
258
277
"""
@@ -379,6 +398,7 @@ def __init__(self, ax, cmap=None,
379
398
extendfrac = None ,
380
399
extendrect = False ,
381
400
label = '' ,
401
+ userax = False ,
382
402
):
383
403
_api .check_isinstance ([colors .Colormap , None ], cmap = cmap )
384
404
_api .check_in_list (
@@ -390,10 +410,8 @@ def __init__(self, ax, cmap=None,
390
410
['uniform' , 'proportional' ], spacing = spacing )
391
411
392
412
# wrap the axes so that it can be positioned as an inset axes:
393
- ax = ColorbarAxes (ax )
413
+ ax = ColorbarAxes (ax , userax = userax )
394
414
self .ax = ax
395
- # Bind some methods to the axes to warn users against using them.
396
- ax .set_xticks = ax .set_yticks = _set_ticks_on_axis_warn
397
415
ax .set (navigate = False )
398
416
399
417
if cmap is None :
0 commit comments