Skip to content

Commit

Permalink
Merge pull request #97 from esc24/exner_fix
Browse files Browse the repository at this point in the history
Axis unit labels
  • Loading branch information
rhattersley committed Oct 3, 2012
2 parents df848e2 + c52a617 commit eed7687
Show file tree
Hide file tree
Showing 20 changed files with 56 additions and 31 deletions.
60 changes: 33 additions & 27 deletions docs/iris/example_code/graphics/deriving_phenomena.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -28,52 +33,53 @@ 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)
cont = qplt.contourf(air_temp_slice)
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


Expand Down
27 changes: 23 additions & 4 deletions lib/iris/quickplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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)

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit eed7687

Please sign in to comment.