Skip to content

Commit 77462d2

Browse files
authored
Scalar Scatter Plot (#4616)
* add failing test * pass test * initial review actions * test 2nd arg scalar * whatsnew
1 parent 3820ae2 commit 77462d2

3 files changed

Lines changed: 50 additions & 2 deletions

File tree

docs/src/whatsnew/dev.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ This document explains the changes made to Iris for this release
4141
#. `@rcomer`_ reverted part of the change from :pull:`3906` so that
4242
:func:`iris.plot.plot` no longer defaults to placing a "Y" coordinate (e.g.
4343
latitude) on the y-axis of the plot. (:issue:`4493`, :pull:`4601`)
44+
45+
#. `@rcomer`_ enabled passing of scalar objects to :func:`~iris.plot.plot` and
46+
:func:`~iris.plot.scatter`. (:pull:`4616`)
4447

4548

4649
💣 Incompatible Changes

lib/iris/plot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,13 +652,13 @@ def _get_plot_objects(args):
652652
u_object, v_object = args[:2]
653653
u, v = _uv_from_u_object_v_object(u_object, v_object)
654654
args = args[2:]
655-
if len(u) != len(v):
655+
if u.size != v.size:
656656
msg = (
657657
"The x and y-axis objects are not compatible. They should "
658658
"have equal sizes but got ({}: {}) and ({}: {})."
659659
)
660660
raise ValueError(
661-
msg.format(u_object.name(), len(u), v_object.name(), len(v))
661+
msg.format(u_object.name(), u.size, v_object.name(), v.size)
662662
)
663663
else:
664664
# single argument
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright Iris contributors
2+
#
3+
# This file is part of Iris and is released under the LGPL license.
4+
# See COPYING and COPYING.LESSER in the root of the repository for full
5+
# licensing details.
6+
"""Unit tests for the `iris.plot._get_plot_objects` function."""
7+
8+
# Import iris.tests first so that some things can be initialised before
9+
# importing anything else.
10+
import iris.tests as tests # isort:skip
11+
12+
import iris.cube
13+
14+
if tests.MPL_AVAILABLE:
15+
from iris.plot import _get_plot_objects
16+
17+
18+
@tests.skip_plot
19+
class Test__get_plot_objects(tests.IrisTest):
20+
def test_scalar(self):
21+
cube1 = iris.cube.Cube(1)
22+
cube2 = iris.cube.Cube(1)
23+
expected = (cube1, cube2, 1, 1, ())
24+
result = _get_plot_objects((cube1, cube2))
25+
self.assertTupleEqual(expected, result)
26+
27+
def test_mismatched_size_first_scalar(self):
28+
cube1 = iris.cube.Cube(1)
29+
cube2 = iris.cube.Cube([1, 42])
30+
with self.assertRaisesRegex(
31+
ValueError, "x and y-axis objects are not compatible"
32+
):
33+
_get_plot_objects((cube1, cube2))
34+
35+
def test_mismatched_size_second_scalar(self):
36+
cube1 = iris.cube.Cube(1)
37+
cube2 = iris.cube.Cube([1, 42])
38+
with self.assertRaisesRegex(
39+
ValueError, "x and y-axis objects are not compatible"
40+
):
41+
_get_plot_objects((cube2, cube1))
42+
43+
44+
if __name__ == "__main__":
45+
tests.main()

0 commit comments

Comments
 (0)