Skip to content

Commit 7051e7f

Browse files
committed
Reuse colorbar outline and patch when updating the colorbar.
... so that changes to the outline edgecolor are kept.
1 parent 3c67db3 commit 7051e7f

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

lib/matplotlib/colorbar.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,19 @@ def __init__(self, ax, cmap=None,
444444
self.extendfrac = extendfrac
445445
self.extendrect = extendrect
446446
self.solids = None
447-
self.lines = list()
448-
self.outline = None
449-
self.patch = None
447+
self.lines = []
448+
449+
self.outline = mpatches.Polygon(
450+
np.empty((0, 2)),
451+
edgecolor=mpl.rcParams['axes.edgecolor'], facecolor='none',
452+
linewidth=mpl.rcParams['axes.linewidth'], closed=True, zorder=2)
453+
ax.add_artist(self.outline)
454+
self.outline.set(clip_box=None, clip_path=None)
455+
self.patch = mpatches.Polygon(
456+
np.empty((0, 2)),
457+
color=mpl.rcParams['axes.facecolor'], linewidth=0.01, zorder=-1)
458+
ax.add_artist(self.patch)
459+
450460
self.dividers = None
451461
self.locator = None
452462
self.formatter = None
@@ -709,26 +719,8 @@ def _config_axes(self, X, Y):
709719
ax.update_datalim(xy)
710720
ax.set_xlim(*ax.dataLim.intervalx)
711721
ax.set_ylim(*ax.dataLim.intervaly)
712-
if self.outline is not None:
713-
self.outline.remove()
714-
self.outline = mpatches.Polygon(
715-
xy, edgecolor=mpl.rcParams['axes.edgecolor'],
716-
facecolor='none',
717-
linewidth=mpl.rcParams['axes.linewidth'],
718-
closed=True,
719-
zorder=2)
720-
ax.add_artist(self.outline)
721-
self.outline.set_clip_box(None)
722-
self.outline.set_clip_path(None)
723-
c = mpl.rcParams['axes.facecolor']
724-
if self.patch is not None:
725-
self.patch.remove()
726-
self.patch = mpatches.Polygon(xy, edgecolor=c,
727-
facecolor=c,
728-
linewidth=0.01,
729-
zorder=-1)
730-
ax.add_artist(self.patch)
731-
722+
self.outline.set_xy(xy)
723+
self.patch.set_xy(xy)
732724
self.update_ticks()
733725

734726
def _set_label(self):
@@ -1276,10 +1268,18 @@ def update_bruteforce(self, mappable):
12761268
self.formatter = None
12771269

12781270
# clearing the axes will delete outline, patch, solids, and lines:
1279-
self.outline = None
1280-
self.patch = None
1271+
self.outline = mpatches.Polygon(
1272+
np.empty((0, 2)),
1273+
edgecolor=mpl.rcParams['axes.edgecolor'], facecolor='none',
1274+
linewidth=mpl.rcParams['axes.linewidth'], closed=True, zorder=2)
1275+
self.ax.add_artist(self.outline)
1276+
self.outline.set(clip_box=None, clip_path=None)
1277+
self.patch = mpatches.Polygon(
1278+
np.empty((0, 2)),
1279+
color=mpl.rcParams['axes.facecolor'], linewidth=0.01, zorder=-1)
1280+
self.ax.add_artist(self.patch)
12811281
self.solids = None
1282-
self.lines = list()
1282+
self.lines = []
12831283
self.dividers = None
12841284
self.update_normal(mappable)
12851285
self.draw_all()

lib/matplotlib/tests/test_colorbar.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,13 +497,16 @@ def test_colorbar_scale_reset():
497497
fig, ax = plt.subplots()
498498
pcm = ax.pcolormesh(z, cmap='RdBu_r', rasterized=True)
499499
cbar = fig.colorbar(pcm, ax=ax)
500+
cbar.outline.set_edgecolor('red')
500501
assert cbar.ax.yaxis.get_scale() == 'linear'
501502

502503
pcm.set_norm(LogNorm(vmin=1, vmax=100))
503504
assert cbar.ax.yaxis.get_scale() == 'log'
504505
pcm.set_norm(Normalize(vmin=-20, vmax=20))
505506
assert cbar.ax.yaxis.get_scale() == 'linear'
506507

508+
assert cbar.outline.get_edgecolor() == mcolors.to_rgba('red')
509+
507510

508511
def test_colorbar_get_ticks_2():
509512
with rc_context({'_internal.classic_mode': False}):

0 commit comments

Comments
 (0)