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

Skip to content

Commit f4a02f4

Browse files
authored
Merge pull request #17834 from anntzer/keepcbardividers
Keep using a single dividers LineCollection instance in colorbar.
2 parents 7a7f1ee + 28b69e6 commit f4a02f4

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed
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
"""
@@ -1317,7 +1317,6 @@ def update_bruteforce(self, mappable):
13171317
self.ax.add_artist(self.patch)
13181318
self.solids = None
13191319
self.lines = []
1320-
self.dividers = None
13211320
self.update_normal(mappable)
13221321
self.draw_all()
13231322
if isinstance(self.mappable, contour.ContourSet):
@@ -1646,16 +1645,7 @@ def _add_solids(self, X, Y, C):
16461645

16471646
self.solids_patches = patches
16481647

1649-
if self.dividers is not None:
1650-
self.dividers.remove()
1651-
self.dividers = None
1652-
1653-
if self.drawedges:
1654-
self.dividers = collections.LineCollection(
1655-
self._edges(X, Y),
1656-
colors=(mpl.rcParams['axes.edgecolor'],),
1657-
linewidths=(0.5 * mpl.rcParams['axes.linewidth'],))
1658-
self.ax.add_collection(self.dividers)
1648+
self.dividers.set_segments(self._edges(X, Y) if self.drawedges else [])
16591649

16601650

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

0 commit comments

Comments
 (0)