Skip to content

Commit a442f77

Browse files
committed
Use the cube dimension order to determine the plot axes.
1 parent 9aad938 commit a442f77

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* When coordinates have no well defined plot axis, iris.plot and iris.quickplot routines now use the order of the cube's dimensions to determine the coordinates to plot as the x and y axis of a plot.

lib/iris/plot.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,15 @@ def guess_axis(coord):
186186
coords[axes.index(axis)] = coord
187187

188188
# Re-order the coordinates to achieve the preferred
189-
# horizontal/vertical associations.
189+
# horizontal/vertical associations. If we can't associate
190+
# an axis to order the coordinates, fall back to using the cube dimension
191+
# followed by the name of the coordinate.
190192
def sort_key(coord):
191193
order = {'X': 2, 'T': 1, 'Y': -1, 'Z': -2}
192194
axis = guess_axis(coord)
193-
return (order.get(axis, 0), coord and coord.name())
195+
return (order.get(axis, 0),
196+
coords.index(coord),
197+
coord and coord.name())
194198
sorted_coords = sorted(coords, key=sort_key)
195199

196200
transpose = (sorted_coords != coords)

lib/iris/tests/test_mapping.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def setUp(self):
102102
self.cube = cube
103103

104104
def test_simple(self):
105-
iplt.contourf(self.cube)
105+
iplt.contourf(self.cube, coords=['y', 'x'])
106106
self.check_graphic()
107107

108108

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# (C) British Crown Copyright 2017, Met Office
2+
#
3+
# This file is part of Iris.
4+
#
5+
# Iris is free software: you can redistribute it and/or modify it under
6+
# the terms of the GNU Lesser General Public License as published by the
7+
# Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# Iris is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU Lesser General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU Lesser General Public License
16+
# along with Iris. If not, see <http://www.gnu.org/licenses/>.
17+
"""Unit tests for the `iris.plot._get_plot_defn` function."""
18+
19+
from __future__ import (absolute_import, division, print_function)
20+
from six.moves import (filter, input, map, range, zip) # noqa
21+
22+
# Import iris.tests first so that some things can be initialised before
23+
# importing anything else.
24+
import iris.tests as tests
25+
26+
import iris.coords
27+
from iris.tests.stock import simple_2d
28+
29+
if tests.MPL_AVAILABLE:
30+
import iris.plot as iplt
31+
32+
33+
@tests.skip_plot
34+
class Test_get_plot_defn(tests.IrisTest):
35+
def test_axis_order_xy(self):
36+
cube_xy = simple_2d()
37+
defn = iplt._get_plot_defn(cube_xy, iris.coords.POINT_MODE)
38+
self.assertEqual([coord.name() for coord in defn.coords],
39+
['bar', 'foo'])
40+
41+
def test_axis_order_yx(self):
42+
cube_yx = simple_2d()
43+
cube_yx.transpose()
44+
defn = iplt._get_plot_defn(cube_yx, iris.coords.POINT_MODE)
45+
self.assertEqual([coord.name() for coord in defn.coords],
46+
['foo', 'bar'])
47+
48+
49+
if __name__ == "__main__":
50+
tests.main()

0 commit comments

Comments
 (0)