Skip to content

Commit 16fad26

Browse files
committed
Make Colorbar outline into a Spine.
This moves its draw to after all artists, which is consistent with normal Axes frames.
1 parent 8511771 commit 16fad26

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
``Colorbar`` outline is now a ``Spine``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
The outline of `~matplotlib.colorbar.Colorbar` is now a `.Spine` and drawn as
5+
one, instead of a `.Polygon` drawn as an artist. This ensures it will always
6+
be drawn after all artists, consistent with Spines on normal Axes.

lib/matplotlib/colorbar.py

+29-13
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import matplotlib.cm as cm
4646
import matplotlib.patches as mpatches
4747
import matplotlib.path as mpath
48+
import matplotlib.spines as mspines
4849
import matplotlib.ticker as ticker
4950
import matplotlib.transforms as mtransforms
5051
import matplotlib._layoutbox as layoutbox
@@ -321,6 +322,27 @@ def tick_values(self, vmin, vmax):
321322
return ticks
322323

323324

325+
class _ColorbarSpine(mspines.Spine):
326+
def __init__(self, axes):
327+
super().__init__(axes, 'colorbar',
328+
mpath.Path(np.empty((0, 2)), closed=True))
329+
330+
def get_window_extent(self, renderer=None):
331+
# This Spine has no Axis associated with it, and doesn't need to adjust
332+
# its location, so we can directly get the window extent from the
333+
# super-super-class.
334+
return mpatches.Patch.get_window_extent(self, renderer=renderer)
335+
336+
def set_xy(self, xy):
337+
self._path = mpath.Path(xy, closed=True)
338+
self.stale = True
339+
340+
def draw(self, renderer):
341+
ret = mpatches.Patch.draw(self, renderer)
342+
self.stale = False
343+
return ret
344+
345+
324346
class ColorbarBase:
325347
r"""
326348
Draw a colorbar in an existing axes.
@@ -427,7 +449,7 @@ def __init__(self, ax, cmap=None,
427449
self.ax = ax
428450
# Bind some methods to the axes to warn users against using them.
429451
ax.set_xticks = ax.set_yticks = _set_ticks_on_axis_warn
430-
ax.set(frame_on=False, navigate=False)
452+
ax.set(navigate=False)
431453

432454
if cmap is None:
433455
cmap = cm.get_cmap()
@@ -457,12 +479,9 @@ def __init__(self, ax, cmap=None,
457479
self.solids = None
458480
self.lines = []
459481

460-
self.outline = mpatches.Polygon(
461-
np.empty((0, 2)),
462-
edgecolor=mpl.rcParams['axes.edgecolor'], facecolor='none',
463-
linewidth=mpl.rcParams['axes.linewidth'], closed=True, zorder=2)
464-
ax.add_artist(self.outline)
465-
self.outline.set(clip_box=None, clip_path=None)
482+
for spine in ax.spines.values():
483+
spine.set_visible(False)
484+
self.outline = ax.spines['outline'] = _ColorbarSpine(ax)
466485

467486
self.patch = mpatches.Polygon(
468487
np.empty((0, 2)),
@@ -1304,12 +1323,9 @@ def update_bruteforce(self, mappable):
13041323
self.formatter = None
13051324

13061325
# clearing the axes will delete outline, patch, solids, and lines:
1307-
self.outline = mpatches.Polygon(
1308-
np.empty((0, 2)),
1309-
edgecolor=mpl.rcParams['axes.edgecolor'], facecolor='none',
1310-
linewidth=mpl.rcParams['axes.linewidth'], closed=True, zorder=2)
1311-
self.ax.add_artist(self.outline)
1312-
self.outline.set(clip_box=None, clip_path=None)
1326+
for spine in self.ax.spines.values():
1327+
spine.set_visible(False)
1328+
self.outline = self.ax.spines['outline'] = _ColorbarSpine(self.ax)
13131329
self.patch = mpatches.Polygon(
13141330
np.empty((0, 2)),
13151331
color=mpl.rcParams['axes.facecolor'], linewidth=0.01, zorder=-1)

0 commit comments

Comments
 (0)