Skip to content

Keep using a single dividers LineCollection instance in colorbar. #17834

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
Jul 5, 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
5 changes: 5 additions & 0 deletions doc/api/next_api_changes/behavior/17834-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
``Colorbar.dividers`` changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This attribute is now always a `.LineCollection` -- an empty one if
``drawedges`` is False. Its default colors and linewidth (:rc:`axes.edgecolor`,
:rc:`axes.linewidth`) are now resolved at instantiation time, not at draw time.
42 changes: 16 additions & 26 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,9 @@ class ColorbarBase:
ax : `~matplotlib.axes.Axes`
The `~.axes.Axes` instance in which the colorbar is drawn.
lines : list
A list of `.LineCollection` if lines were drawn, otherwise
an empty list.
A list of `.LineCollection` (empty if no lines were drawn).
dividers : `.LineCollection`
A LineCollection if *drawedges* is ``True``, otherwise ``None``.
A LineCollection (empty if *drawedges* is ``False``).
Copy link
Member

Choose a reason for hiding this comment

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

Should this get an API change note?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think this is an API break because previously the attribute could be LineCollection or None, and now it's always LineCollection, so we don't add more types to handle.

Copy link
Member

Choose a reason for hiding this comment

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

Technically, people could have checked cb.dividers is None, which now would have to be changed to not cb.dividers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure, added changelog


Parameters
----------
Expand Down Expand Up @@ -464,12 +463,18 @@ def __init__(self, ax, cmap=None,
linewidth=mpl.rcParams['axes.linewidth'], closed=True, zorder=2)
ax.add_artist(self.outline)
self.outline.set(clip_box=None, clip_path=None)

self.patch = mpatches.Polygon(
np.empty((0, 2)),
color=mpl.rcParams['axes.facecolor'], linewidth=0.01, zorder=-1)
ax.add_artist(self.patch)

self.dividers = None
self.dividers = collections.LineCollection(
[],
colors=[mpl.rcParams['axes.edgecolor']],
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this change the time of rcParams resolution from draw-time to init?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The same "change in resolution time" occurred in #15981, where it is arguably a bugfix.
But sure, added to the changelog.

linewidths=[0.5 * mpl.rcParams['axes.linewidth']])
self.ax.add_collection(self.dividers)

self.locator = None
self.formatter = None
self._manual_tick_data_values = None
Expand Down Expand Up @@ -819,18 +824,13 @@ def _add_solids(self, X, Y, C):
if self.solids is not None:
self.solids.remove()
self.solids = col
if self.dividers is not None:
self.dividers.remove()
self.dividers = None

if self.drawedges:
linewidths = (0.5 * mpl.rcParams['axes.linewidth'],)
self.dividers = collections.LineCollection(
self._edges(X, Y),
colors=(mpl.rcParams['axes.edgecolor'],),
linewidths=linewidths)
self.ax.add_collection(self.dividers)
elif len(self._y) >= self.n_rasterize:
self.solids.set_rasterized(True)
self.dividers.set_segments(self._edges(X, Y))
else:
self.dividers.set_segments([])
if len(self._y) >= self.n_rasterize:
self.solids.set_rasterized(True)

def add_lines(self, levels, colors, linewidths, erase=True):
"""
Expand Down Expand Up @@ -1335,7 +1335,6 @@ def update_bruteforce(self, mappable):
self.ax.add_artist(self.patch)
self.solids = None
self.lines = []
self.dividers = None
self.update_normal(mappable)
self.draw_all()
if isinstance(self.mappable, contour.ContourSet):
Expand Down Expand Up @@ -1664,16 +1663,7 @@ def _add_solids(self, X, Y, C):

self.solids_patches = patches

if self.dividers is not None:
self.dividers.remove()
self.dividers = None

if self.drawedges:
self.dividers = collections.LineCollection(
self._edges(X, Y),
colors=(mpl.rcParams['axes.edgecolor'],),
linewidths=(0.5 * mpl.rcParams['axes.linewidth'],))
self.ax.add_collection(self.dividers)
self.dividers.set_segments(self._edges(X, Y) if self.drawedges else [])


def colorbar_factory(cax, mappable, **kwargs):
Expand Down