Visitar URL original
MNT: Refactor to make colorbar Axes patching more clear by timhoffm · Pull Request #32427 · matplotlib/matplotlib · GitHub
Skip to content
Open
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
34 changes: 19 additions & 15 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,13 +426,7 @@ def __init__(
isinstance(self.mappable, contour.ContourSet)):
self.ax.set_navigate(False)

# These are the functions that set up interactivity on this colorbar
self._interactive_funcs = ["_get_view", "_set_view",
"_set_view_from_bbox", "drag_pan"]
for x in self._interactive_funcs:
setattr(self.ax, x, getattr(self, x))
# Set the cla function to the cbar's method to override it
self.ax.cla = self._cbar_cla
self._patch_ax_methods()
# Callbacks for the extend calculations to handle inverting the axis
self._extend_cid1 = self.ax.callbacks.connect(
"xlim_changed", self._do_extends)
Expand All @@ -447,6 +441,24 @@ def __init__(
self._ax_remove = self.ax._remove_method
self.ax._remove_method = functools.partial(_remove_cbar_axes, cbar=self)

def _patch_ax_methods(self):
"""Override Axes methods because colorbars behave slightly different."""
# These are the functions that set up interactivity on this colorbar
self._interactive_funcs = ["_get_view", "_set_view",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we storing this on the object? This isn't dynamic, entered elsewhere, so I think it should just be a local convenience variable.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no strong opinion, as-is, a class attribute, global constant or hard-coded values would work all, and I don’t see any significant advantage in any of the solutions, which is why I kept the status quo.

"_set_view_from_bbox", "drag_pan"]
for x in self._interactive_funcs:
setattr(self.ax, x, getattr(self, x))
# Override cla Set the cla function to the cbar's method to override it
self.ax.cla = self._cbar_cla

def _cbar_cla(self):
"""Function to clear the interactive colorbar state."""
for x in self._interactive_funcs:
delattr(self.ax, x)
# We now restore the old cla() back and can call it directly
del self.ax.cla
self.ax.cla()

@property
def long_axis(self):
"""Axis that has decorations (ticks, etc) on it."""
Expand Down Expand Up @@ -494,14 +506,6 @@ def minorformatter(self, fmt):
self.long_axis.set_minor_formatter(fmt)
self._minorformatter = fmt

def _cbar_cla(self):
"""Function to clear the interactive colorbar state."""
for x in self._interactive_funcs:
delattr(self.ax, x)
# We now restore the old cla() back and can call it directly
del self.ax.cla
self.ax.cla()

def update_normal(self, mappable=None):
"""
Update solid patches, lines, etc.
Expand Down
Loading