Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bed37f7

Browse filesBrowse files
committed
Merge pull request #3110 from KevKeating/iss2688_delcolorbar
BUG: Add Figure.delcolorbar() to fully delete a colorbar
2 parents 0b481b5 + 262c33a commit bed37f7
Copy full SHA for bed37f7

File tree

Expand file treeCollapse file tree

3 files changed

+75
-2
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+75
-2
lines changed

‎CHANGELOG

Copy file name to clipboardExpand all lines: CHANGELOG
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2014-06-10 Added Colorbar.remove()
2+
13
2014-06-07 Fixed bug so radial plots can be saved as ps in py3k.
24

35
2014-06-01 Changed the fmt kwarg of errorbar to support the

‎lib/matplotlib/colorbar.py

Copy file name to clipboardExpand all lines: lib/matplotlib/colorbar.py
+37-1Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,14 @@ def _locate(self, x):
840840
def set_alpha(self, alpha):
841841
self.alpha = alpha
842842

843+
def remove(self):
844+
"""
845+
Remove this colorbar from the figure
846+
"""
847+
848+
fig = self.ax.figure
849+
fig.delaxes(self.ax)
850+
843851

844852
class Colorbar(ColorbarBase):
845853
"""
@@ -967,6 +975,33 @@ def update_bruteforce(self, mappable):
967975
# be recalculating everything if there was a simple alpha
968976
# change.
969977

978+
def remove(self):
979+
"""
980+
Remove this colorbar from the figure. If the colorbar was created with
981+
``use_gridspec=True`` then restore the gridspec to its previous value.
982+
"""
983+
984+
ColorbarBase.remove(self)
985+
self.mappable.callbacksSM.disconnect(self.mappable.colorbar_cid)
986+
self.mappable.colorbar = None
987+
self.mappable.colorbar_cid = None
988+
989+
try:
990+
ax = self.mappable.axes
991+
except AttributeError:
992+
return
993+
994+
try:
995+
gs = ax.get_subplotspec().get_gridspec()
996+
subplotspec = gs.get_topmost_subplotspec()
997+
except AttributeError:
998+
# use_gridspec was False
999+
pos = ax.get_position(original=True)
1000+
ax.set_position(pos)
1001+
else:
1002+
# use_gridspec was True
1003+
ax.set_subplotspec(subplotspec)
1004+
9701005

9711006
@docstring.Substitution(make_axes_kw_doc)
9721007
def make_axes(parents, location=None, orientation=None, fraction=0.15,
@@ -1280,7 +1315,8 @@ def colorbar_factory(cax, mappable, **kwargs):
12801315
else:
12811316
cb = Colorbar(cax, mappable, **kwargs)
12821317

1283-
mappable.callbacksSM.connect('changed', cb.on_mappable_changed)
1318+
cid = mappable.callbacksSM.connect('changed', cb.on_mappable_changed)
12841319
mappable.colorbar = cb
1320+
mappable.colorbar_cid = cid
12851321

12861322
return cb

‎lib/matplotlib/tests/test_colorbar.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_colorbar.py
+36-1Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import numpy as np
77
from numpy import ma
88
import matplotlib
9-
from matplotlib.testing.decorators import image_comparison, knownfailureif
9+
from matplotlib.testing.decorators import image_comparison, cleanup
1010
import matplotlib.pyplot as plt
1111
from matplotlib import rcParams
1212
from matplotlib.colors import BoundaryNorm
@@ -211,6 +211,41 @@ def test_colorbar_single_scatter():
211211
plt.colorbar(cs)
212212

213213

214+
def _test_remove_from_figure(use_gridspec):
215+
"""
216+
Test `remove_from_figure` with the specified ``use_gridspec`` setting
217+
"""
218+
fig = plt.figure()
219+
ax = fig.add_subplot(111)
220+
sc = ax.scatter([1, 2], [3, 4], cmap="spring")
221+
sc.set_array(np.array([5, 6]))
222+
pre_figbox = np.array(ax.figbox)
223+
cb = fig.colorbar(sc, use_gridspec=use_gridspec)
224+
fig.subplots_adjust()
225+
cb.remove()
226+
fig.subplots_adjust()
227+
post_figbox = np.array(ax.figbox)
228+
assert (pre_figbox == post_figbox).all()
229+
230+
231+
@cleanup
232+
def test_remove_from_figure_with_gridspec():
233+
"""
234+
Make sure that `remove_from_figure` removes the colorbar and properly
235+
restores the gridspec
236+
"""
237+
_test_remove_from_figure(True)
238+
239+
240+
@cleanup
241+
def test_remove_from_figure_no_gridspec():
242+
"""
243+
Make sure that `remove_from_figure` removes a colorbar that was created
244+
without modifying the gridspec
245+
"""
246+
_test_remove_from_figure(False)
247+
248+
214249
if __name__ == '__main__':
215250
import nose
216251
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.