Skip to content

Commit

Permalink
Scalar Scatter Plot (#4616)
Browse files Browse the repository at this point in the history
* add failing test

* pass test

* initial review actions

* test 2nd arg scalar

* whatsnew
  • Loading branch information
rcomer authored Mar 8, 2022
1 parent 3820ae2 commit 77462d2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
3 changes: 3 additions & 0 deletions docs/src/whatsnew/dev.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/iris/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions lib/iris/tests/unit/plot/test__get_plot_objects.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 77462d2

Please sign in to comment.