3838from matplotlib import _api , collections , cm , colors , contour , ticker
3939from matplotlib .axes ._base import _TransformedBoundsLocator
4040from matplotlib .axes ._axes import Axes
41+ from matplotlib .axes ._subplots import SubplotBase
4142import matplotlib .artist as martist
4243import matplotlib .patches as mpatches
4344import matplotlib .path as mpath
@@ -235,24 +236,44 @@ class ColorbarAxes(Axes):
235236 Users should not normally instantiate this class, but it is the class
236237 returned by ``cbar = fig.colorbar(im); cax = cbar.ax``.
237238 """
238- def __init__ (self , parent ):
239+ def __init__ (self , parent , userax = False ):
239240 """
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.
240247 """
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 ])
245261 self .__dict__ = inner_ax .__dict__
246- self .parent_ax = parent
262+
263+ self .parent_ax = parent_ax
247264 self .inner_ax = inner_ax
248265 self .parent_ax .xaxis .set_visible (False )
249266 self .parent_ax .yaxis .set_visible (False )
250267 self .parent_ax .set_facecolor ('none' )
251- # map some features to the parent so users have access...
252268 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
254271 for attr in ["get_position" , "set_position" , "set_aspect" ]:
255272 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+
256277
257278 def _set_inner_bounds (self , bounds ):
258279 """
@@ -379,6 +400,7 @@ def __init__(self, ax, cmap=None,
379400 extendfrac = None ,
380401 extendrect = False ,
381402 label = '' ,
403+ userax = False ,
382404 ):
383405 _api .check_isinstance ([colors .Colormap , None ], cmap = cmap )
384406 _api .check_in_list (
@@ -390,10 +412,8 @@ def __init__(self, ax, cmap=None,
390412 ['uniform' , 'proportional' ], spacing = spacing )
391413
392414 # wrap the axes so that it can be positioned as an inset axes:
393- ax = ColorbarAxes (ax )
415+ ax = ColorbarAxes (ax , userax = userax )
394416 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
397417 ax .set (navigate = False )
398418
399419 if cmap is None :
0 commit comments