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

Skip to content

Commit fb768c7

Browse files
committed
Deal with axes_grid differences
1 parent f084fcb commit fb768c7

File tree

2 files changed

+20
-25
lines changed

2 files changed

+20
-25
lines changed

lib/matplotlib/colorbar.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,19 @@ def __init__(self, parent, userax=True):
239239
parent._axes.add_child_axes(outer_ax)
240240
outer_ax._axes.child_axes.remove(parent)
241241
else:
242-
parent.remove()
242+
try:
243+
parent.remove()
244+
except ValueError:
245+
pass # Already removed
243246
else:
244247
outer_ax = parent
245248

246-
# swap axes in the stack:
247-
fig._localaxes.remove(outer_ax)
248-
fig._axstack.remove(outer_ax)
249-
fig._localaxes.add(self)
250-
fig._axstack.add(self)
249+
# swap axes in the stack if its in there:
250+
if outer_ax in fig._localaxes:
251+
fig._localaxes.remove(outer_ax)
252+
fig._axstack.remove(outer_ax)
253+
fig._localaxes.add(self)
254+
fig._axstack.add(self)
251255
inner_ax = outer_ax.inset_axes([0, 0, 1, 1])
252256
self.__dict__.update(inner_ax.__dict__)
253257

@@ -261,8 +265,8 @@ def __init__(self, parent, userax=True):
261265
self.outer_ax.set_xticks = self.inner_ax.set_xticks
262266
self.outer_ax.set_yticks = self.inner_ax.set_yticks
263267
for attr in ["get_position", "set_aspect",
264-
"_remove_method", "_set_position",
265-
"set_position"]:
268+
"_remove_method", "_set_position",
269+
"set_position", "cla", "draw"]:
266270
setattr(self, attr, getattr(self.outer_ax, attr))
267271
self._colorbar_info = None # used for mpl-created axes
268272
if hasattr(self.outer_ax, "get_subplotspec"):
@@ -272,7 +276,11 @@ def __init__(self, parent, userax=True):
272276
if userax:
273277
self._colorbar_info = 'user'
274278
# point the parent's methods all at this axes...
279+
origdict = parent.__dict__
275280
parent.__dict__ = self.__dict__
281+
for key in origdict.keys():
282+
if key not in parent.__dict__:
283+
parent.__dict__[key] = origdict[key]
276284

277285
def _set_inner_bounds(self, bounds):
278286
"""
@@ -281,9 +289,6 @@ def _set_inner_bounds(self, bounds):
281289
self.inner_ax._axes_locator = _TransformedBoundsLocator(
282290
bounds, self.outer_ax.transAxes)
283291

284-
def draw(self, renderer):
285-
self.outer_ax.draw(renderer)
286-
287292

288293
class _ColorbarSpine(mspines.Spine):
289294
def __init__(self, axes):

lib/mpl_toolkits/axes_grid1/axes_grid.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import numpy as np
55

6-
import matplotlib as mpl
76
from matplotlib import _api
87
from matplotlib.gridspec import SubplotSpec
98

@@ -29,28 +28,19 @@ def colorbar(self, mappable, *, ticks=None, **kwargs):
2928
orientation = (
3029
"horizontal" if self.orientation in ["top", "bottom"] else
3130
"vertical")
32-
kwargs['userax'] = False
33-
cb = mpl.colorbar.Colorbar(
34-
self, mappable, orientation=orientation, ticks=ticks, **kwargs)
35-
self._config_axes()
31+
cb = self.figure.colorbar(mappable, cax=self, orientation=orientation,
32+
ticks=ticks, **kwargs)
3633
return cb
3734

38-
def _config_axes(self):
39-
"""Make an axes patch and outline."""
40-
ax = self
41-
ax.set_navigate(False)
42-
ax.axis[:].toggle(all=False)
43-
b = self._default_label_on
44-
ax.axis[self.orientation].toggle(all=b)
45-
4635
def toggle_label(self, b):
4736
self._default_label_on = b
4837
axis = self.axis[self.orientation]
4938
axis.toggle(ticklabels=b, label=b)
5039

5140
def cla(self):
41+
orientation = self.orientation
5242
super().cla()
53-
self._config_axes()
43+
self.orientation = orientation
5444

5545

5646
class CbarAxes(CbarAxesBase, Axes):

0 commit comments

Comments
 (0)