8000 Merge pull request #15981 from anntzer/keepcbaroutline · matplotlib/matplotlib@b42d6d9 · GitHub
[go: up one dir, main page]

Skip to content

Commit b42d6d9

Browse files
authored
Merge pull request #15981 from anntzer/keepcbaroutline
Reuse colorbar outline and patch when updating the colorbar.
2 parents 3909ea2 + 7051e7f commit b42d6d9

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
@@ -719,26 +729,8 @@ def _config_axes(self, X, Y):
719729
ax.update_datalim(xy)
720730
ax.set_xlim(*ax.dataLim.intervalx)
721731
ax.set_ylim(*ax.dataLim.intervaly)
722-
if self.outline is not None:
723-
self.outline.remove()
724-
self.outline = mpatches.Polygon(
725-
xy, edgecolor=mpl.rcParams['axes.edgecolor'],
726-
facecolor='none',
727-
linewidth=mpl.rcParams['axes.linewidth'],
728-
closed=True,
729-
zorder=2)
730-
ax.add_artist(self.outline)
731-
self.outline.set_clip_box(None)
732-
self.outline.set_clip_path(None)
733-
c = mpl.rcParams['axes.facecolor']
734-
if self.patch is not None:
735-
self.patch.remove()
736-
self.patch = mpatches.Polygon(xy, edgecolor=c,
737-
facecolor=c,
738-
linewidth=0.01,
739-
zorder=-1)
740-
ax.add_artist(self.patch)
741-
732+
self.outline.set_xy(xy)
733+
self.patch.set_xy(xy)
742734
self.update_ticks()
743735

744736
def _set_label(self):
@@ -1322,10 +1314,18 @@ def update_bruteforce(self, mappable):
13221314
self.formatter = None
13231315

13241316
# clearing the axes will delete outline, patch, solids, and lines:
1325-
self.outline = None
1326-
self.patch = None
1317+
self.outline = mpatches.Polygon(
1318+
np.empty((0, 2)),
1319+
edgecolor=mpl.rcParams['axes.edgecolor'], facecolor='none',
1320+
linewidth=mpl.rcParams['axes.linewidth'], closed=True, zorder=2)
1321+
self.ax.add_artist(self.outline)
1322+
self.outline.set(clip_box=None, clip_path=None)
1323+
self.patch = mpatches.Polygon(
1324+
np.empty((0, 2)),
1325+
color=mpl.rcParams['axes.facecolor'], linewidth=0.01, zorder=-1)
1326+
self.ax.add_artist(self.patch)
13271327
self.solids = None
1328-
self.lines = list()
1328+
self.lines = []
13291329
self.dividers = None
13301330
self.update_normal(mappable)
13311331
self.draw_all()

lib/matplotlib/tests/test_colorbar.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,13 +522,16 @@ def test_colorbar_scale_reset():
522522
fig, ax = plt.subplots()
523523
pcm = ax.pcolormesh(z, cmap='RdBu_r', rasterized=True)
524524
cbar = fig.colorbar(pcm, ax=ax)
525+
cbar.outline.set_edgecolor('red')
525526
assert cbar.ax.yaxis< 7442 /span>.get_scale() == 'linear'
526527

527528
pcm.set_norm(LogNorm(vmin=1, vmax=100))
528529
assert cbar.ax.yaxis.get_scale() == 'log'
529530
pcm.set_norm(Normalize(vmin=-20, vmax=20))
530531
assert cbar.ax.yaxis.get_scale() == 'linear'
531532

533+
assert cbar.outline.get_edgecolor() == mcolors.to_rgba('red')
534+
532535

533536
def test_colorbar_get_ticks_2():
534537
with rc_context({'_internal.classic_mode': False}):

0 commit comments

Comments
 (0)
0