Skip to content

Latest commit

 

History

History
328 lines (232 loc) · 11.4 KB

plotting_a_cube.rst

File metadata and controls

328 lines (232 loc) · 11.4 KB

Plotting a Cube

Iris utilises the power of Python's Matplotlib package in order to generate high quality, production ready 1D and 2D plots. The functionality of the Matplotlib pyplot module has been extended within Iris to facilitate easy visualisation of a cube's data.

Matplotlib's Pyplot Basics

A simple line plot can be created using the :pymatplotlib.pyplot.plot function:

import matplotlib.pyplot as plt
plt.plot([1, 2, 2.5])
plt.show()

This code will automatically create a figure with appropriate axes for the plot and show it on screen. The call to plt.plot([1, 2, 2.5]) will create a line plot with appropriate axes for the data (x=0, y=1; x=1, y=2; x=2, y=2.5). The call to plt.show() tells Matplotlib that you have finished with this plot and that you would like to visualise it in a window. This is an example of using matplotlib in non-interactive mode.

There are two modes of rendering within Matplotlib; interactive and non-interactive.

Interactive Plot Rendering

The previous example was non-interactive as the figure is only rendered after the call to :pyplt.show() <matplotlib.pyplot.show>. Rendering plots interactively can be achieved by changing the interactive mode:

import matplotlib.pyplot as plt
plt.interactive(True)
plt.plot([1, 2, 2.5])

In this case the plot is rendered automatically with no need to explicitly call :pymatplotlib.pyplot.show after plt.plot. Subsequent changes to your figure will be automatically rendered in the window.

The current rendering mode can be determined as follows:

import matplotlib.pyplot as plt
print(plt.isinteractive())

Note

For clarity, each example includes all of the imports required to run on its own; when combining examples such as the two above, it would not be necessary to repeat the import statement more than once:

import matplotlib.pyplot as plt
plt.interactive(True)
plt.plot([1, 2, 2.5])
print(plt.isinteractive())

Interactive mode does not clear out the figure buffer, so figures have to be explicitly closed when they are finished with:

plt.close()

-- or just close the figure window.

Interactive mode sometimes requires an extra draw command to update all changes, which can be done with:

plt.draw()

For the remainder of this tutorial we will work in non-interactive mode, so ensure that interactive mode is turned off with:

plt.interactive(False)

Saving a Plot

The :pymatplotlib.pyplot.savefig function is similar to plt.show() in that they are both non-interactive visualisation modes. As you might expect, plt.savefig saves your figure as an image:

import matplotlib.pyplot as plt
plt.plot([1, 2, 2.5])
plt.savefig('plot123.png')

The filename extension passed to the :pymatplotlib.pyplot.savefig function can be used to control the output file format of the plot (keywords can also be used to control this and other aspects, see :pymatplotlib.pyplot.savefig).

Some of the formats which are supported by plt.savefig:

Format Type Description
EPS Vector Encapsulated PostScript
PDF Vector Portable Document Format
PNG Raster Portable Network Graphics, a format with a lossless compression method
PS Vector PostScript, ideal for printer output
SVG Vector Scalable Vector Graphics, XML based

Iris Cube Plotting

The Iris modules :pyiris.quickplot and :pyiris.plot extend the Matplotlib pyplot interface by implementing thin wrapper functions. These wrapper functions simply bridge the gap between an Iris cube and the data expected by standard Matplotlib pyplot functions. This means that all Matplotlib pyplot functionality, including keyword options, are still available through the Iris plotting wrapper functions.

As a rule of thumb:

  • if you wish to do a visualisation with a cube, use iris.plot or iris.quickplot.
  • if you wish to show, save or manipulate any visualisation, including ones created with Iris, use matplotlib.pyplot.
  • if you wish to create a non cube visualisation, also use matplotlib.pyplot.

The iris.quickplot module is exactly the same as the iris.plot module, except that quickplot will add a title, x and y labels and a colorbar where appropriate.

Note

In all subsequent examples the matplotlib.pyplot, iris.plot and iris.quickplot modules are imported as plt, iplt and qplt respectively in order to make the code more readable. This is equivalent to:

import matplotlib.pyplot as plt
import iris.plot as iplt
import iris.quickplot as qplt

Plotting 1-Dimensional Cubes

The simplest 1D plot is achieved with the :pyiris.plot.plot function. The syntax is very similar to that which you would provide to Matplotlib's equivalent :pymatplotlib.pyplot.plot and indeed all of the keyword arguments are equivalent:

userguide/plotting_examples/1d_simple.py

For more information on how this example reduced the 2D cube to 1 dimension see the previous section entitled subsetting_a_cube.

Note

Axis labels and a plot title can be added using the plt.title() <matplotlib.pyplot.title>, plt.xlabel() <matplotlib.pyplot.xlabel> and plt.ylabel() <matplotlib.pyplot.ylabel> functions.

As well as providing simple Matplotlib wrappers, Iris also has a :pyiris.quickplot module, which adds extra cube based metadata to a plot. For example, the previous plot can be improved quickly by replacing iris.plot with iris.quickplot:

userguide/plotting_examples/1d_quickplot_simple.py

Multi-Line Plot

A multi-lined (or over-plotted) plot, with a legend, can be achieved easily by calling iris.plot.plot or iris.quickplot.plot consecutively and providing the label keyword to identify it. Once all of the lines have been added the matplotlib.pyplot.legend function can be called to indicate that a legend is desired:

../gallery_code/general/plot_lineplot_with_legend.py

This example of consecutive qplt.plot calls coupled with the Cube.slices() <iris.cube.Cube.slices> method on a cube shows the temperature at some latitude cross-sections.

Note

The previous example uses the if __name__ == "__main__" style to run the desired code if and only if the script is run from the command line.

This is a good habit to get into when writing scripts in Python as it means that any useful functions or variables defined within the script can be imported into other scripts without running all of the code and thus creating an unwanted plot. This is discussed in more detail at http://effbot.org/pyfaq/tutor-what-is-if-name-main-for.htm.

In order to run this example, you will need to copy the code into a file and run it using python my_file.py.

Plotting 2-Dimensional Cubes

Creating Maps

Whenever a 2D plot is created using an iris.coord_systems.CoordSystem, a cartopy ~cartopy.mpl.GeoAxes instance is created, which can be accessed with the matplotlib.pyplot.gca function.

Given the current map, you can draw gridlines and coastlines amongst other things.

cartopy's gridlines() <cartopy.mpl.GeoAxes.gridlines>, cartopy's coastlines() <cartopy.mpl.GeoAxes.coastlines>.

Cube Contour

A simple contour plot of a cube can be created with either the iris.plot.contour or iris.quickplot.contour functions:

userguide/plotting_examples/cube_contour.py

Cube Filled Contour

Similarly a filled contour plot of a cube can be created with the iris.plot.contourf or iris.quickplot.contourf functions:

userguide/plotting_examples/cube_contourf.py

Cube Block Plot

In some situations the underlying coordinates are better represented with a continuous bounded coordinate, in which case a "block" plot may be more appropriate. Continuous block plots can be achieved with either iris.plot.pcolormesh or iris.quickplot.pcolormesh.

Note

If the cube's coordinates do not have bounds, iris.plot.pcolormesh and iris.quickplot.pcolormesh will attempt to guess suitable values based on their points (see also iris.coords.Coord.guess_bounds()).

userguide/plotting_examples/cube_blockplot.py

Brewer Colour Palettes

Iris includes colour specifications and designs developed by Cynthia Brewer These colour schemes are freely available under the following licence:

Apache-Style Software License for ColorBrewer software and ColorBrewer Color Schemes

Copyright (c) 2002 Cynthia Brewer, Mark Harrower, and The Pennsylvania State University.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

To include a reference in a journal article or report please refer to section 5 in the citation guidance provided by Cynthia Brewer.

For adding citations to Iris plots, see brewer-cite (below).

Available Brewer Schemes

The following subset of Brewer palettes found at colorbrewer2.org are available within Iris.

userguide/plotting_examples/brewer.py

Plotting With Brewer

To plot a cube using a Brewer colour palette, simply select one of the Iris registered Brewer colour palettes and plot the cube as normal. The Brewer palettes become available once iris.plot or iris.quickplot are imported.

userguide/plotting_examples/cube_brewer_contourf.py

Adding a Citation

Citations can be easily added to a plot using the iris.plot.citation function. The recommended text for the Cynthia Brewer citation is provided by iris.plot.BREWER_CITE.

userguide/plotting_examples/cube_brewer_cite_contourf.py