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

Skip to content

Commit 28b69e6

Browse files
committed
Keep using a single dividers LineCollection instance in colorbar.
... updating its data as needed, instead of repeatedly deleting it and instantiating new ones.
1 parent dbea6f8 commit 28b69e6

2 files changed

Lines changed: 21 additions & 26 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``Colorbar.dividers`` changes
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
This attribute is now always a `.LineCollection` -- an empty one if
4+
``drawedges`` is False. Its default colors and linewidth (:rc:`axes.edgecolor`,
5+
:rc:`axes.linewidth`) are now resolved at instantiation time, not at draw time.

lib/matplotlib/colorbar.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,9 @@ class ColorbarBase:
353353
ax : `~matplotlib.axes.Axes`
354354
The `~.axes.Axes` instance in which the colorbar is drawn.
355355
lines : list
356-
A list of `.LineCollection` if lines were drawn, otherwise
357-
an empty list.
356+
A list of `.LineCollection` (empty if no lines were drawn).
358357
dividers : `.LineCollection`
359-
A LineCollection if *drawedges* is ``True``, otherwise ``None``.
358+
A LineCollection (empty if *drawedges* is ``False``).
360359
361360
Parameters
362361
----------
@@ -464,12 +463,18 @@ def __init__(self, ax, cmap=None,
464463
linewidth=mpl.rcParams['axes.linewidth'], closed=True, zorder=2)
465464
ax.add_artist(self.outline)
466465
self.outline.set(clip_box=None, clip_path=None)
466+
467467
self.patch = mpatches.Polygon(
468468
np.empty((0, 2)),
469469
color=mpl.rcParams['axes.facecolor'], linewidth=0.01, zorder=-1)
470470
ax.add_artist(self.patch)
471471

472-
self.dividers = None
472+
self.dividers = collections.LineCollection(
473+
[],
474+
colors=[mpl.rcParams['axes.edgecolor']],
475+
linewidths=[0.5 * mpl.rcParams['axes.linewidth']])
476+
self.ax.add_collection(self.dividers)
477+
473478
self.locator = None
474479
self.formatter = None
475480
self._manual_tick_data_values = None
@@ -819,18 +824,13 @@ def _add_solids(self, X, Y, C):
819824
if self.solids is not None:
820825
self.solids.remove()
821826
self.solids = col
822-
if self.dividers is not None:
823-
self.dividers.remove()
824-
self.dividers = None
827+
825828
if self.drawedges:
826-
linewidths = (0.5 * mpl.rcParams['axes.linewidth'],)
827-
self.dividers = collections.LineCollection(
828-
self._edges(X, Y),
829-
colors=(mpl.rcParams['axes.edgecolor'],),
830-
linewidths=linewidths)
831-
self.ax.add_collection(self.dividers)
832-
elif len(self._y) >= self.n_rasterize:
833-
self.solids.set_rasterized(True)
829+
self.dividers.set_segments(self._edges(X, Y))
830+
else:
831+
self.dividers.set_segments([])
832+
if len(self._y) >= self.n_rasterize:
833+
self.solids.set_rasterized(True)
834834

835835
def add_lines(self, levels, colors, linewidths, erase=True):
836836
"""
@@ -1335,7 +1335,6 @@ def update_bruteforce(self, mappable):
13351335
self.ax.add_artist(self.patch)
13361336
self.solids = None
13371337
self.lines = []
1338-
self.dividers = None
13391338
self.update_normal(mappable)
13401339
self.draw_all()
13411340
if isinstance(self.mappable, contour.ContourSet):
@@ -1664,16 +1663,7 @@ def _add_solids(self, X, Y, C):
16641663

16651664
self.solids_patches = patches
16661665

1667-
if self.dividers is not None:
1668-
self.dividers.remove()
1669-
self.dividers = None
1670-
1671-
if self.drawedges:
1672-
self.dividers = collections.LineCollection(
1673-
self._edges(X, Y),
1674-
colors=(mpl.rcParams['axes.edgecolor'],),
1675-
linewidths=(0.5 * mpl.rcParams['axes.linewidth'],))
1676-
self.ax.add_collection(self.dividers)
1666+
self.dividers.set_segments(self._edges(X, Y) if self.drawedges else [])
16771667

16781668

16791669
def colorbar_factory(cax, mappable, **kwargs):

0 commit comments

Comments
 (0)