Skip to content

Commit 889836e

Browse files
committed
Incorporate final review improvements to related 'anomaly_log_colouring' example.
1 parent d781df8 commit 889836e

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

docs/iris/example_code/graphics/anomaly_log_contours.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
the appropriate choice of contouring levels.
3131
3232
"""
33+
import cartopy.crs as ccrs
3334
import iris
35+
import iris.coord_categorisation
3436
import iris.plot as iplt
3537
import matplotlib.pyplot as plt
3638
import matplotlib.colors as mcols
@@ -82,17 +84,21 @@ def main():
8284
file_path = iris.sample_data_path('E1_north_america.nc')
8385
temperatures = iris.load_cube(file_path)
8486

85-
# Create a sample anomaly field for one year, by subtracting a time mean.
86-
i_year = 122
87+
# Create a year-number coordinate from the time information.
88+
iris.coord_categorisation.add_year(temperatures, 'time')
89+
90+
# Create a sample anomaly field for one chosen year, by extracting that
91+
# year and subtracting the time mean.
92+
sample_year = 1982
93+
year_temperature = temperatures.extract(iris.Constraint(year=sample_year))
8794
time_mean = temperatures.collapsed('time', iris.analysis.MEAN)
88-
anomaly = temperatures[i_year] - time_mean
95+
anomaly = year_temperature - time_mean
8996

9097
# Construct a plot title string explaining which years are involved.
91-
times = temperatures.coord('time')
92-
cube_years = [time.year for time in times.units.num2date(times.points)]
93-
title = 'Temperature anomaly [{}, log scale]'.format(anomaly.units)
94-
title += '\n{} differences from {}-{} average.'.format(
95-
cube_years[i_year], cube_years[0], cube_years[-1])
98+
years = temperatures.coord('year').points
99+
plot_title = 'Temperature anomaly'
100+
plot_title += '\n{} differences from {}-{} average.'.format(
101+
sample_year, years[0], years[-1])
96102

97103
# Define the levels we want to contour with.
98104
# NOTE: these will also appear as the colorbar ticks.
@@ -103,6 +109,9 @@ def main():
103109
colour_minus='#0040c0',
104110
colour_plus='darkred')
105111

112+
# Create an Axes, specifying the map projection.
113+
plt.axes(projection=ccrs.LambertConformal())
114+
106115
# Make a contour plot with these levels and colours.
107116
contours = iplt.contourf(anomaly, contour_levels,
108117
colors=layer_colours,
@@ -111,13 +120,16 @@ def main():
111120
# with the min and max colours.
112121

113122
# Add a colourbar.
114-
plt.colorbar(contours, orientation='horizontal')
123+
bar = plt.colorbar(contours, orientation='horizontal')
115124
# NOTE: This picks up the 'extend=both' from the plot, automatically
116125
# showing how out-of-range values are handled.
117126

127+
# Label the colourbar to show the units.
128+
bar.set_label('[{}, log scale]'.format(anomaly.units))
129+
118130
# Add coastlines and a title.
119131
plt.gca().coastlines()
120-
plt.title(title)
132+
plt.title(plot_title)
121133

122134
# Display the result.
123135
plt.show()
-841 Bytes
Loading

0 commit comments

Comments
 (0)