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