8000 Reuse colorbar outline and patch when updating the colorbar. by anntzer · Pull Request #15981 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Reuse colorbar outline and patch when updating the colorbar. #15981

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
Mar 14, 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
52 changes: 26 additions & 26 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,19 @@ def __init__(self, ax, cmap=None,
self.extendfrac = extendfrac
self.extendrect = extendrect
self.solids = None
self.lines = list()
self.outline = None
self.patch = None
self.lines = []

self.outline = mpatches.Polygon(
np.empty((0, 2)),
edgecolor=mpl.rcParams['axes.edgecolor'], facecolor='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.locator = None
self.formatter = None
Expand Down Expand Up @@ -709,26 +719,8 @@ def _config_axes(self, X, Y):
ax.update_datalim(xy)
ax.set_xlim(*ax.dataLim.intervalx)
ax.set_ylim(*ax.dataLim.intervaly)
if self.outline is not None:
self.outline.re 8000 move()
self.outline = mpatches.Polygon(
xy, edgecolor=mpl.rcParams['axes.edgecolor'],
facecolor='none',
linewidth=mpl.rcParams['axes.linewidth'],
closed=True,
zorder=2)
ax.add_artist(self.outline)
self.outline.set_clip_box(None)
self.outline.set_clip_path(None)
c = mpl.rcParams['axes.facecolor']
if self.patch is not None:
self.patch.remove()
self.patch = mpatches.Polygon(xy, edgecolor=c,
facecolor=c,
linewidth=0.01,
zorder=-1)
ax.add_artist(self.patch)

self.outline.set_xy(xy)
self.patch.set_xy(xy)
self.update_ticks()

def _set_label(self):
Expand Down Expand Up @@ -1276,10 +1268,18 @@ def update_bruteforce(self, mappable):
self.formatter = None

# clearing the axes will delete outline, patch, solids, and lines:
self.outline = None
self.patch = None
self.outline = mpatches.Polygon(
np.empty((0, 2)),
edgecolor=mpl.rcParams['axes.edgecolor'], facecolor='none',
linewidth=mpl.rcParams['axes.linewidth'], closed=True, zorder=2)
self.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)
self.ax.add_artist(self.patch)
Comment on lines +1271 to +1280
Copy link
Member

Choose a reason for hiding this comment

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

This code is repeated from __init__. Should this be moved to a private helper to remove duplication? Maybe only returning the outline and patch without adding them to ax within the helper?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is getting deprecated in #15982.

self.solids = None
self.lines = list()
self.lines = []
self.dividers = None
self.update_normal(mappable)
self.draw_all()
Expand Down
3 changes: 3 additions & 0 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,16 @@ def test_colorbar_scale_reset():
fig, ax = plt.subplots()
pcm = ax.pcolormesh(z, cmap='RdBu_r', rasterized=True)
cbar = fig.colorbar(pcm, ax=ax)
cbar.outline.set_edgecolor('red')
assert cbar.ax.yaxis.get_scale() == 'linear'

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

assert cbar.outline.get_edgecolor() == mcolors.to_rgba('red')


def test_colorbar_get_ticks_2():
with rc_context({'_internal.classic_mode': False}):
Expand Down
0