Skip to content

Commit c4d9d89

Browse files
krikrubjlittle
andauthored
Prevent color bar from being misplaced (#4894)
* Prevent color bar from being misplaced Currently, if `plt.gca()` gives a different Axes object than `axes` in the function `_label`, the color bar will be malpositioned, ending up either on another subfigure, or in another figure window altogether. Specifying the `ax` argument when calling `plt.colorbar` prevents this. * add whatsnew and test coverage * check axes and figure * tidy and add extra axes2 test Co-authored-by: Bill Little <[email protected]>
1 parent 5cd21bf commit c4d9d89

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

docs/src/whatsnew/latest.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ This document explains the changes made to Iris for this release
2525
📢 Announcements
2626
================
2727

28-
#. N/A
28+
#. Welcome to `@krikru`_ who made their first contribution to Iris 🎉
2929

3030

3131
✨ Features
@@ -136,6 +136,11 @@ This document explains the changes made to Iris for this release
136136
array type. This prevents masks being lost in some cases and therefore
137137
resolves :issue:`2987`. (:pull:`3790`)
138138

139+
#. `@krikru`_ and `@rcomer`_ updated :mod:`iris.quickplot` such that the
140+
colorbar is added to the correct ``axes`` when specified as a keyword
141+
argument to a plotting routine. Otherwise, by default the colorbar will be
142+
added to the current axes of the current figure. (:pull:`4894`)
143+
139144
#. `@rcomer`_ and `@bjlittle`_ (reviewer) modified :func:`iris.util.mask_cube` so it
140145
either works in place or returns a new cube (:issue:`3717`, :pull:`4889`)
141146

@@ -279,6 +284,7 @@ This document explains the changes made to Iris for this release
279284
core dev names are automatically included by the common_links.inc:
280285
281286
.. _@evertrol: https://github.com/evertrol
287+
.. _@krikru: https://github.com/krikru
282288

283289

284290
.. comment

lib/iris/quickplot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def _label(cube, mode, result=None, ndims=2, coords=None, axes=None):
7171
if result is not None:
7272
draw_edges = mode == iris.coords.POINT_MODE
7373
bar = plt.colorbar(
74-
result, orientation="horizontal", drawedges=draw_edges
74+
result, ax=axes, orientation="horizontal", drawedges=draw_edges
7575
)
7676
has_known_units = not (
7777
cube.units.is_unknown() or cube.units.is_no_unit()

lib/iris/tests/test_quickplot.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,5 +247,39 @@ def test_not_reference_time_units(self):
247247
self.check_graphic()
248248

249249

250+
@tests.skip_data
251+
@tests.skip_plot
252+
class TestSubplotColorbar(tests.IrisTest):
253+
def setUp(self):
254+
theta = _load_theta()
255+
coords = ["model_level_number", "grid_longitude"]
256+
self.data = next(theta.slices(coords))
257+
spec = (1, 1, 1)
258+
self.figure1 = plt.figure()
259+
self.axes1 = self.figure1.add_subplot(*spec)
260+
self.figure2 = plt.figure()
261+
self.axes2 = self.figure2.add_subplot(*spec)
262+
263+
def _check(self, mappable, figure, axes):
264+
self.assertIs(mappable.axes, axes)
265+
self.assertIs(mappable.colorbar.mappable, mappable)
266+
self.assertIs(mappable.colorbar.ax.get_figure(), figure)
267+
268+
def test_with_axes1(self):
269+
# plot using the first figure subplot axes (explicit)
270+
mappable = qplt.contourf(self.data, axes=self.axes1)
271+
self._check(mappable, self.figure1, self.axes1)
272+
273+
def test_with_axes2(self):
274+
# plot using the second figure subplot axes (explicit)
275+
mappable = qplt.contourf(self.data, axes=self.axes2)
276+
self._check(mappable, self.figure2, self.axes2)
277+
278+
def test_without_axes__default(self):
279+
# plot using the second/last figure subplot axes (default)
280+
mappable = qplt.contourf(self.data)
281+
self._check(mappable, self.figure2, self.axes2)
282+
283+
250284
if __name__ == "__main__":
251285
tests.main()

0 commit comments

Comments
 (0)