Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4999aa7

Browse filesBrowse files
committed
FIX: make manual axes more like normal
1 parent 127619d commit 4999aa7
Copy full SHA for 4999aa7

File tree

Expand file treeCollapse file tree

2 files changed

+40
-13
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+40
-13
lines changed

‎lib/matplotlib/colorbar.py

Copy file name to clipboardExpand all lines: lib/matplotlib/colorbar.py
+31-11Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from matplotlib import _api, collections, cm, colors, contour, ticker
3939
from matplotlib.axes._base import _TransformedBoundsLocator
4040
from matplotlib.axes._axes import Axes
41+
from matplotlib.axes._subplots import SubplotBase
4142
import matplotlib.artist as martist
4243
import matplotlib.patches as mpatches
4344
import 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:

‎lib/matplotlib/figure.py

Copy file name to clipboardExpand all lines: lib/matplotlib/figure.py
+9-2Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.