diff --git a/docs/src/whatsnew/dev.rst b/docs/src/whatsnew/dev.rst index b9d5989bfc..5cc0769c57 100644 --- a/docs/src/whatsnew/dev.rst +++ b/docs/src/whatsnew/dev.rst @@ -41,6 +41,9 @@ This document explains the changes made to Iris for this release #. `@rcomer`_ reverted part of the change from :pull:`3906` so that :func:`iris.plot.plot` no longer defaults to placing a "Y" coordinate (e.g. latitude) on the y-axis of the plot. (:issue:`4493`, :pull:`4601`) + +#. `@rcomer`_ enabled passing of scalar objects to :func:`~iris.plot.plot` and + :func:`~iris.plot.scatter`. (:pull:`4616`) 💣 Incompatible Changes diff --git a/lib/iris/plot.py b/lib/iris/plot.py index aefca889cf..d886ac1cf9 100644 --- a/lib/iris/plot.py +++ b/lib/iris/plot.py @@ -652,13 +652,13 @@ def _get_plot_objects(args): u_object, v_object = args[:2] u, v = _uv_from_u_object_v_object(u_object, v_object) args = args[2:] - if len(u) != len(v): + if u.size != v.size: msg = ( "The x and y-axis objects are not compatible. They should " "have equal sizes but got ({}: {}) and ({}: {})." ) raise ValueError( - msg.format(u_object.name(), len(u), v_object.name(), len(v)) + msg.format(u_object.name(), u.size, v_object.name(), v.size) ) else: # single argument diff --git a/lib/iris/tests/unit/plot/test__get_plot_objects.py b/lib/iris/tests/unit/plot/test__get_plot_objects.py new file mode 100644 index 0000000000..8586faa756 --- /dev/null +++ b/lib/iris/tests/unit/plot/test__get_plot_objects.py @@ -0,0 +1,45 @@ +# Copyright Iris contributors +# +# This file is part of Iris and is released under the LGPL license. +# See COPYING and COPYING.LESSER in the root of the repository for full +# licensing details. +"""Unit tests for the `iris.plot._get_plot_objects` function.""" + +# Import iris.tests first so that some things can be initialised before +# importing anything else. +import iris.tests as tests # isort:skip + +import iris.cube + +if tests.MPL_AVAILABLE: + from iris.plot import _get_plot_objects + + +@tests.skip_plot +class Test__get_plot_objects(tests.IrisTest): + def test_scalar(self): + cube1 = iris.cube.Cube(1) + cube2 = iris.cube.Cube(1) + expected = (cube1, cube2, 1, 1, ()) + result = _get_plot_objects((cube1, cube2)) + self.assertTupleEqual(expected, result) + + def test_mismatched_size_first_scalar(self): + cube1 = iris.cube.Cube(1) + cube2 = iris.cube.Cube([1, 42]) + with self.assertRaisesRegex( + ValueError, "x and y-axis objects are not compatible" + ): + _get_plot_objects((cube1, cube2)) + + def test_mismatched_size_second_scalar(self): + cube1 = iris.cube.Cube(1) + cube2 = iris.cube.Cube([1, 42]) + with self.assertRaisesRegex( + ValueError, "x and y-axis objects are not compatible" + ): + _get_plot_objects((cube2, cube1)) + + +if __name__ == "__main__": + tests.main()