Skip to content

Make Colorbar outline into a Spine. #18320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/api/next_api_changes/behavior/18320-ES.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
``Colorbar`` outline is now a ``Spine``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The outline of `~matplotlib.colorbar.Colorbar` is now a `.Spine` and drawn as
one, instead of a `.Polygon` drawn as an artist. This ensures it will always
be drawn after all artists, consistent with Spines on normal Axes.
42 changes: 29 additions & 13 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import matplotlib.cm as cm
import matplotlib.patches as mpatches
import matplotlib.path as mpath
import matplotlib.spines as mspines
import matplotlib.ticker as ticker
import matplotlib.transforms as mtransforms
import matplotlib._layoutbox as layoutbox
Expand Down Expand Up @@ -321,6 +322,27 @@ def tick_values(self, vmin, vmax):
return ticks


class _ColorbarSpine(mspines.Spine):
def __init__(self, axes):
super().__init__(axes, 'colorbar',
mpath.Path(np.empty((0, 2)), closed=True))

def get_window_extent(self, renderer=None):
# This Spine has no Axis associated with it, and doesn't need to adjust
# its location, so we can directly get the window extent from the
# super-super-class.
return mpatches.Patch.get_window_extent(self, renderer=renderer)

def set_xy(self, xy):
self._path = mpath.Path(xy, closed=True)
self.stale = True

def draw(self, renderer):
ret = mpatches.Patch.draw(self, renderer)
self.stale = False
return ret


class ColorbarBase:
r"""
Draw a colorbar in an existing axes.
Expand Down Expand Up @@ -427,7 +449,7 @@ def __init__(self, ax, cmap=None,
self.ax = ax
# Bind some methods to the axes to warn users against using them.
ax.set_xticks = ax.set_yticks = _set_ticks_on_axis_warn
ax.set(frame_on=False, navigate=False)
ax.set(navigate=False)

if cmap is None:
cmap = cm.get_cmap()
Expand Down Expand Up @@ -457,12 +479,9 @@ def __init__(self, ax, cmap=None,
self.solids = None
self.lines = []

self.outline = mpatches.Polygon(
np.empty((0, 2)),
edgecolor=mpl.rcParams['axes.edgecolor'], facecolor='none',
linewidth=mpl.rcParams['axes.linewidth'], closed=True, zorder=2)
ax.add_artist(self.outline)
self.outline.set(clip_box=None, clip_path=None)
for spine in ax.spines.values():
spine.set_visible(False)
self.outline = ax.spines['outline'] = _ColorbarSpine(ax)

self.patch = mpatches.Polygon(
np.empty((0, 2)),
Expand Down Expand Up @@ -1304,12 +1323,9 @@ def update_bruteforce(self, mappable):
self.formatter = None

# clearing the axes will delete outline, patch, solids, and lines:
self.outline = mpatches.Polygon(
np.empty((0, 2)),
edgecolor=mpl.rcParams['axes.edgecolor'], facecolor='none',
linewidth=mpl.rcParams['axes.linewidth'], closed=True, zorder=2)
self.ax.add_artist(self.outline)
self.outline.set(clip_box=None, clip_path=None)
for spine in self.ax.spines.values():
spine.set_visible(False)
self.outline = self.ax.spines['outline'] = _ColorbarSpine(self.ax)
self.patch = mpatches.Polygon(
np.empty((0, 2)),
color=mpl.rcParams['axes.facecolor'], linewidth=0.01, zorder=-1)
Expand Down