diff --git a/docs/iris/example_code/graphics/deriving_phenomena.py b/docs/iris/example_code/graphics/deriving_phenomena.py index 9e4d91aa61..8a80cd888f 100644 --- a/docs/iris/example_code/graphics/deriving_phenomena.py +++ b/docs/iris/example_code/graphics/deriving_phenomena.py @@ -2,11 +2,11 @@ Deriving Exner Pressure and Air Temperature =========================================== -This example shows some processing of cubes in order to derive further related cubes; in this case -the derived cubes are Exner pressure and air temperature which are calculated by combining air pressure, -air potential temperature and specific humidity. Finally, the two new cubes are presented side-by-side in -a plot. - +This example shows some processing of cubes in order to derive further related +cubes; in this case the derived cubes are Exner pressure and air temperature +which are calculated by combining air pressure, air potential temperature and +specific humidity. Finally, the two new cubes are presented side-by-side in a +plot. """ import itertools @@ -15,11 +15,16 @@ import iris import iris.coords as coords +import iris.iterate import iris.quickplot as qplt def limit_colorbar_ticks(contour_object): - """Takes a contour object which has an associated colorbar and limits the number of ticks on the colorbar to 4.""" + """ + Takes a contour object which has an associated colorbar and limits the + number of ticks on the colorbar to 4. + + """ colorbar = contour_object.colorbar[0] colorbar.locator = matplotlib.ticker.MaxNLocator(4) colorbar.update_ticks() @@ -28,44 +33,44 @@ def limit_colorbar_ticks(contour_object): def main(): fname = iris.sample_data_path('colpex.pp') - # the list of phenomena of interest + # The list of phenomena of interest phenomena = ['air_potential_temperature', 'air_pressure'] - # define the constraint on standard name and model level - constraints = [iris.Constraint(phenom, model_level_number=1) for phenom in phenomena] + # Define the constraint on standard name and model level + constraints = [iris.Constraint(phenom, model_level_number=1) for + phenom in phenomena] air_potential_temperature, air_pressure = iris.load_cubes(fname, constraints) - # define a coordinate which represents 1000 hPa + # Define a coordinate which represents 1000 hPa p0 = coords.AuxCoord(100000, long_name='P0', units='Pa') - # calculate Exner pressure + # Calculate Exner pressure exner_pressure = (air_pressure / p0) ** (287.05 / 1005.0) - # set the standard name (the unit is scalar) + # Set the name (the unit is scalar) exner_pressure.rename('exner_pressure') - - # calculate air_temp + # Calculate air_temp air_temperature = exner_pressure * air_potential_temperature - # set phenomenon definition and unit - air_temperature.standard_name = 'air_temperature' - air_temperature.units = 'K' + # Set the name (the unit is K) + air_temperature.rename('air_temperature') - - # Now create an iterator which will give us lat lon slices of exner pressure and air temperature in - # the form [exner_slice, air_temp_slice] - lat_lon_slice_pairs = itertools.izip( - exner_pressure.slices(['grid_latitude', 'grid_longitude']), - air_temperature.slices(['grid_latitude', 'grid_longitude']) - ) - plt.figure(figsize=(8, 4)) + # Now create an iterator which will give us lat lon slices of + # exner pressure and air temperature in the form + # (exner_slice, air_temp_slice). + lat_lon_slice_pairs = iris.iterate.izip(exner_pressure, + air_temperature, + coords=['grid_latitude', + 'grid_longitude']) + plt.figure(figsize=(8, 4)) for exner_slice, air_temp_slice in lat_lon_slice_pairs: plt.subplot(121) cont = qplt.contourf(exner_slice) - # The default colorbar has a few too many ticks on it, causing text to overlap. Therefore, limit the number of ticks + # The default colorbar has a few too many ticks on it, causing text to + # overlap. Therefore, limit the number of ticks. limit_colorbar_ticks(cont) plt.subplot(122) @@ -73,7 +78,8 @@ def main(): limit_colorbar_ticks(cont) plt.show() - # For the purposes of this example, break after the first loop - we only want to demonstrate the first plot + # For the purposes of this example, break after the first loop - we + # only want to demonstrate the first plot. break diff --git a/lib/iris/quickplot.py b/lib/iris/quickplot.py index b23da7bd9c..0a62d53f82 100644 --- a/lib/iris/quickplot.py +++ b/lib/iris/quickplot.py @@ -36,8 +36,19 @@ def _title(cube_or_coord, with_units): title = '' else: title = cube_or_coord.name().replace('_', ' ').capitalize() - if with_units: - title += ' / {}'.format(cube_or_coord.units) + units = cube_or_coord.units + if with_units and not (units.unknown or + units.no_unit or + units == iris.unit.Unit('1')): + + # For non-time units use the shortest unit representation e.g. + # prefer 'K' over 'kelvin', but not '0.0174532925199433 rad' + # over 'degrees' + if (not units.is_time() and not units.time_reference and + len(units.symbol) < len(str(units))): + units = units.symbol + title += ' / {}'.format(units) + return title @@ -48,8 +59,16 @@ def _label(cube, mode, result=None, ndims=2, coords=None): if result is not None: draw_edges = mode == iris.coords.POINT_MODE - bar = plt.colorbar(result, orientation='horizontal', drawedges=draw_edges) - bar.set_label(cube.units) + bar = plt.colorbar(result, orientation='horizontal', + drawedges=draw_edges) + has_known_units = not (cube.units.unknown or cube.units.no_unit) + if has_known_units and cube.units != iris.unit.Unit('1'): + # Use shortest unit representation for anything other than time + if (not cube.units.is_time() and not cube.units.time_reference and + len(cube.units.symbol) < len(str(cube.units))): + bar.set_label(cube.units.symbol) + else: + bar.set_label(cube.units) # Remove the tick which is put on the colorbar by default. bar.ax.tick_params(length=0) diff --git a/lib/iris/tests/results/visual_tests/test_cross_section.TestCrossSection.test_cross_section.1.png b/lib/iris/tests/results/visual_tests/test_cross_section.TestCrossSection.test_cross_section.1.png index 52704882bc..b6626a9615 100644 Binary files a/lib/iris/tests/results/visual_tests/test_cross_section.TestCrossSection.test_cross_section.1.png and b/lib/iris/tests/results/visual_tests/test_cross_section.TestCrossSection.test_cross_section.1.png differ diff --git a/lib/iris/tests/results/visual_tests/test_deriving_phenomena.TestDerivingPhenomena.test_deriving_phenomena.0.png b/lib/iris/tests/results/visual_tests/test_deriving_phenomena.TestDerivingPhenomena.test_deriving_phenomena.0.png index 391dddeaf7..584600ed2d 100644 Binary files a/lib/iris/tests/results/visual_tests/test_deriving_phenomena.TestDerivingPhenomena.test_deriving_phenomena.0.png and b/lib/iris/tests/results/visual_tests/test_deriving_phenomena.TestDerivingPhenomena.test_deriving_phenomena.0.png differ diff --git a/lib/iris/tests/results/visual_tests/test_plot.TestQuickplotPlot.test_t.0.png b/lib/iris/tests/results/visual_tests/test_plot.TestQuickplotPlot.test_t.0.png index 7ce68b6359..efb8ec12aa 100644 Binary files a/lib/iris/tests/results/visual_tests/test_plot.TestQuickplotPlot.test_t.0.png and b/lib/iris/tests/results/visual_tests/test_plot.TestQuickplotPlot.test_t.0.png differ diff --git a/lib/iris/tests/results/visual_tests/test_plot.TestQuickplotPlot.test_t_dates.0.png b/lib/iris/tests/results/visual_tests/test_plot.TestQuickplotPlot.test_t_dates.0.png index 48392d94e2..4c4d5eb158 100644 Binary files a/lib/iris/tests/results/visual_tests/test_plot.TestQuickplotPlot.test_t_dates.0.png and b/lib/iris/tests/results/visual_tests/test_plot.TestQuickplotPlot.test_t_dates.0.png differ diff --git a/lib/iris/tests/results/visual_tests/test_plot.TestQuickplotPlot.test_x.0.png b/lib/iris/tests/results/visual_tests/test_plot.TestQuickplotPlot.test_x.0.png index 1cac00b406..95013ebe74 100644 Binary files a/lib/iris/tests/results/visual_tests/test_plot.TestQuickplotPlot.test_x.0.png and b/lib/iris/tests/results/visual_tests/test_plot.TestQuickplotPlot.test_x.0.png differ diff --git a/lib/iris/tests/results/visual_tests/test_plot.TestQuickplotPlot.test_y.0.png b/lib/iris/tests/results/visual_tests/test_plot.TestQuickplotPlot.test_y.0.png index 9e041e5659..ff6ae37108 100644 Binary files a/lib/iris/tests/results/visual_tests/test_plot.TestQuickplotPlot.test_y.0.png and b/lib/iris/tests/results/visual_tests/test_plot.TestQuickplotPlot.test_y.0.png differ diff --git a/lib/iris/tests/results/visual_tests/test_plot.TestQuickplotPlot.test_z.0.png b/lib/iris/tests/results/visual_tests/test_plot.TestQuickplotPlot.test_z.0.png index 3698a2c599..f137a8b516 100644 Binary files a/lib/iris/tests/results/visual_tests/test_plot.TestQuickplotPlot.test_z.0.png and b/lib/iris/tests/results/visual_tests/test_plot.TestQuickplotPlot.test_z.0.png differ diff --git a/lib/iris/tests/results/visual_tests/test_quickplot.TestLabels.test_contour.1.png b/lib/iris/tests/results/visual_tests/test_quickplot.TestLabels.test_contour.1.png index f6e6d721d3..4971d541e4 100644 Binary files a/lib/iris/tests/results/visual_tests/test_quickplot.TestLabels.test_contour.1.png and b/lib/iris/tests/results/visual_tests/test_quickplot.TestLabels.test_contour.1.png differ diff --git a/lib/iris/tests/results/visual_tests/test_quickplot.TestLabels.test_contourf.1.png b/lib/iris/tests/results/visual_tests/test_quickplot.TestLabels.test_contourf.1.png index f6e6d721d3..4971d541e4 100644 Binary files a/lib/iris/tests/results/visual_tests/test_quickplot.TestLabels.test_contourf.1.png and b/lib/iris/tests/results/visual_tests/test_quickplot.TestLabels.test_contourf.1.png differ diff --git a/lib/iris/tests/results/visual_tests/test_quickplot.TestLabels.test_contourf.2.png b/lib/iris/tests/results/visual_tests/test_quickplot.TestLabels.test_contourf.2.png index 88307bdf1c..e76eb692eb 100644 Binary files a/lib/iris/tests/results/visual_tests/test_quickplot.TestLabels.test_contourf.2.png and b/lib/iris/tests/results/visual_tests/test_quickplot.TestLabels.test_contourf.2.png differ diff --git a/lib/iris/tests/results/visual_tests/test_quickplot.TestLabels.test_contourf_nameless.0.png b/lib/iris/tests/results/visual_tests/test_quickplot.TestLabels.test_contourf_nameless.0.png index 89505af148..75a20703a2 100644 Binary files a/lib/iris/tests/results/visual_tests/test_quickplot.TestLabels.test_contourf_nameless.0.png and b/lib/iris/tests/results/visual_tests/test_quickplot.TestLabels.test_contourf_nameless.0.png differ diff --git a/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_non_cube_coordinate.0.png b/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_non_cube_coordinate.0.png index b76ec0785e..a9fd4d7ff3 100644 Binary files a/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_non_cube_coordinate.0.png and b/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_non_cube_coordinate.0.png differ diff --git a/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.0.png b/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.0.png index fafbd4950e..cc8e870a65 100644 Binary files a/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.0.png and b/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.0.png differ diff --git a/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.1.png b/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.1.png index 52704882bc..b6626a9615 100644 Binary files a/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.1.png and b/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.1.png differ diff --git a/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.2.png b/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.2.png index 6f56ccb935..8e1ab15d9d 100644 Binary files a/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.2.png and b/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.2.png differ diff --git a/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.3.png b/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.3.png index 48eb336430..b82edd431b 100644 Binary files a/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.3.png and b/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.3.png differ diff --git a/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.4.png b/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.4.png index b6a08616c7..9f0eeff6ff 100644 Binary files a/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.4.png and b/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.4.png differ diff --git a/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.5.png b/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.5.png index 51ad4fcdf9..9a7a09f92c 100644 Binary files a/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.5.png and b/lib/iris/tests/results/visual_tests/test_quickplot.TestQuickplotCoordinatesGiven.test_zx.5.png differ