diff --git a/docs/iris/src/whatsnew/contributions_2.1/newfeature_2018-Feb-14_cube_plot_with_vertical.txt b/docs/iris/src/whatsnew/contributions_2.1/newfeature_2018-Feb-14_cube_plot_with_vertical.txt new file mode 100644 index 0000000000..2dfe646228 --- /dev/null +++ b/docs/iris/src/whatsnew/contributions_2.1/newfeature_2018-Feb-14_cube_plot_with_vertical.txt @@ -0,0 +1 @@ +* iris.plot.plot an iris.quickplot.plot now automatically place the cube on the x axis if the primary coordinate being plotted against is a vertical coordinate. (e.g. ``iris.plot.plot(z_cube)`` would produce an z vs phenomenon plot, where before it would have produced a phenomenon vs z one) diff --git a/lib/iris/plot.py b/lib/iris/plot.py index 177b78000e..ffdedd5214 100644 --- a/lib/iris/plot.py +++ b/lib/iris/plot.py @@ -1,4 +1,4 @@ -# (C) British Crown Copyright 2010 - 2017, Met Office +# (C) British Crown Copyright 2010 - 2018, Met Office # # This file is part of Iris. # @@ -483,7 +483,19 @@ def _get_plot_objects(args): # single argument v_object = args[0] u_object = _u_object_from_v_object(v_object) + u, v = _uv_from_u_object_v_object(u_object, args[0]) + + # If a single cube argument, and the associated dimension coordinate + # is vertical-like, put the coordinate on the y axis, and the data o + # the x. + if (isinstance(v_object, iris.cube.Cube) and + iris.util.guess_coord_axis(u_object) in ['Y', 'Z']): + # If we have a single argument, and it is vertial-like, put it on + # the y axis. + u_object, v_object = v_object, u_object + u, v = v, u + args = args[1:] return u_object, v_object, u, v, args diff --git a/lib/iris/quickplot.py b/lib/iris/quickplot.py index 0557a57cef..0c0fa60af2 100644 --- a/lib/iris/quickplot.py +++ b/lib/iris/quickplot.py @@ -1,4 +1,4 @@ -# (C) British Crown Copyright 2010 - 2016, Met Office +# (C) British Crown Copyright 2010 - 2018, Met Office # # This file is part of Iris. # @@ -127,11 +127,8 @@ def _get_titles(u_object, v_object): def _label_1d_plot(*args): - if len(args) > 1 and isinstance(args[1], - (iris.cube.Cube, iris.coords.Coord)): - xlabel, ylabel, title = _get_titles(*args[:2]) - else: - xlabel, ylabel, title = _get_titles(None, args[0]) + u_obj, v_obj, _, _, _ = iplt._get_plot_objects(args) + xlabel, ylabel, title = _get_titles(u_obj, v_obj) plt.title(title) plt.xlabel(xlabel) plt.ylabel(ylabel) diff --git a/lib/iris/tests/idiff.py b/lib/iris/tests/idiff.py index 919947196f..2c29ebf8b0 100755 --- a/lib/iris/tests/idiff.py +++ b/lib/iris/tests/idiff.py @@ -244,7 +244,7 @@ def step_over_diffs(result_dir, action, display=True): if isinstance(e, ValueError) or \ isinstance(e, ImageComparisonFailure): print('Could not compare {}: {}'.format(result_fname, - e.message)) + str(e))) continue else: # Propagate the exception, keeping the stack trace diff --git a/lib/iris/tests/unit/quickplot/test_plot.py b/lib/iris/tests/unit/quickplot/test_plot.py index cca87c133b..807bc16598 100644 --- a/lib/iris/tests/unit/quickplot/test_plot.py +++ b/lib/iris/tests/unit/quickplot/test_plot.py @@ -1,4 +1,4 @@ -# (C) British Crown Copyright 2014 - 2015, Met Office +# (C) British Crown Copyright 2014 - 2018, Met Office # # This file is part of Iris. # @@ -22,6 +22,7 @@ # Import iris.tests first so that some things can be initialised before # importing anything else. import iris.tests as tests +from iris.tests.stock import simple_2d from iris.tests.unit.plot import TestGraphicStringCoord if tests.MPL_AVAILABLE: @@ -43,5 +44,28 @@ def test_xaxis_labels(self): self.assertPointsTickLabels('xaxis') +class TestAxisLabels(tests.GraphicsTest): + def test_xy_cube(self): + c = simple_2d()[:, 0] + qplt.plot(c) + ax = qplt.plt.gca() + x = ax.xaxis.get_label().get_text() + self.assertEqual(x, 'Bar') + y = ax.yaxis.get_label().get_text() + self.assertEqual(y, 'Thingness') + + def test_yx_cube(self): + c = simple_2d()[:, 0] + c.transpose() + # Making the cube a vertical coordinate should change the default + # orientation of the plot. + c.coord('bar').attributes['positive'] = 'up' + qplt.plot(c) + ax = qplt.plt.gca() + x = ax.xaxis.get_label().get_text() + self.assertEqual(x, 'Thingness') + y = ax.yaxis.get_label().get_text() + self.assertEqual(y, 'Bar') + if __name__ == "__main__": tests.main()