Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6b61028

Browse files
committed
FIX: make manual axes more like normal
1 parent 127619d commit 6b61028

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

lib/matplotlib/colorbar.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -235,24 +235,43 @@ class ColorbarAxes(Axes):
235235
Users should not normally instantiate this class, but it is the class
236236
returned by ``cbar = fig.colorbar(im); cax = cbar.ax``.
237237
"""
238-
def __init__(self, parent):
238+
def __init__(self, parent, userax=False):
239239
"""
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.
240246
"""
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])
245260
self.__dict__ = inner_ax.__dict__
246-
self.parent_ax = parent
261+
262+
self.parent_ax = parent_ax
247263
self.inner_ax = inner_ax
248264
self.parent_ax.xaxis.set_visible(False)
249265
self.parent_ax.yaxis.set_visible(False)
250266
self.parent_ax.set_facecolor('none')
251-
# map some features to the parent so users have access...
252267
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
254270
for attr in ["get_position", "set_position", "set_aspect"]:
255271
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__
256275

257276
def _set_inner_bounds(self, bounds):
258277
"""
@@ -379,6 +398,7 @@ def __init__(self, ax, cmap=None,
379398
extendfrac=None,
380399
extendrect=False,
381400
label='',
401+
userax=False,
382402
):
383403
_api.check_isinstance([colors.Colormap, None], cmap=cmap)
384404
_api.check_in_list(
@@ -390,10 +410,8 @@ def __init__(self, ax, cmap=None,
390410
['uniform', 'proportional'], spacing=spacing)
391411

392412
# wrap the axes so that it can be positioned as an inset axes:
393-
ax = ColorbarAxes(ax)
413+
ax = ColorbarAxes(ax, userax=userax)
394414
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
397415
ax.set(navigate=False)
398416

399417
if cmap is None:

lib/matplotlib/figure.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,21 +1156,28 @@ def colorbar(self, mappable, cax=None, ax=None, use_gridspec=True, **kw):
11561156
"to colorbar().")
11571157

11581158
# Store the value of gca so that we can set it back later on.
1159-
current_ax = self.gca()
11601159
if cax is None:
1160+
current_ax = self.gca()
1161+
userax = False
11611162
if (use_gridspec and isinstance(ax, SubplotBase)
11621163
and not self.get_constrained_layout()):
11631164
cax, kw = cbar.make_axes_gridspec(ax, **kw)
11641165
else:
11651166
cax, kw = cbar.make_axes(ax, **kw)
1167+
else:
1168+
userax = True
11661169

11671170
# need to remove kws that cannot be passed to Colorbar
11681171
NON_COLORBAR_KEYS = ['fraction', 'pad', 'shrink', 'aspect', 'anchor',
11691172
'panchor']
11701173
cb_kw = {k: v for k, v in kw.items() if k not in NON_COLORBAR_KEYS}
1174+
if userax:
1175+
cb_kw['userax'] = True
1176+
11711177
cb = cbar.Colorbar(cax, mappable, **cb_kw)
11721178

1173-
self.sca(current_ax)
1179+
if not userax:
1180+
self.sca(current_ax)
11741181
self.stale = True
11751182
return cb
11761183

0 commit comments

Comments
 (0)