Iris: quickplot.contourf features not attached to supplied axes

Created on 6 Feb 2018  Â·  4Comments  Â·  Source: SciTools/iris

When iris.quickplot adds labels, colorbar, etc. to a contourf plot it doesn't necessarily add them to the supplied axes instance. Example:

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

fig1 = plt.figure()
fig2 = plt.figure()

fname = iris.sample_data_path('hybrid_height.nc')
theta = iris.load_cube(fname, 'air_potential_temperature')
cross_section = next(theta.slices(['grid_longitude',
                                   'model_level_number']))

ax = fig1.add_subplot(2, 1, 1)
qplt.contourf(cross_section, axes=ax)

plt.show()

Output figure 1:
figure_1

Output figure 2:
figure_2

Low Patch Bug

All 4 comments

Hi @rcomer – excellent spot! I've reproduced this behaviour, but it's taken me a little while to decide whether it is desirable or not. In many ways this is expected matplotlib behaviour: the labels have been set on the current figure, which is fig2 for the entirety of your code snippet after L6.

The thing that makes this behaviour both apparent and undesirable is defining two figures and then also passing an Axes instance to the contourf axes kwarg. If this kwarg were not passed then the contourf-plotted data and the labels would all appear on fig2, as would be expected. With the axes kwarg passed the contourf plot of the data is sent off to the requested axes but the labels are not! So they end up being set on the current figure, which is otherwise reasonable, matplotlib behaviour.

I think we can fix this by also passing the axes kwarg, if set, to the labelling function so that in such a case the plotted data and labels all end up on the same Axes / Figure. Problem is this will increase the complexity of all the qplt plotting functions... It's something we should probably do though.

Thanks for the reply @dkillick. This came up for me because I am alternating between figures when making my plots. Something like

for plot_num, fname in enumerate(list_of_data_sets, 1):
    cube = iris.load_cube(fname)
    for fig in [fig1, fig2]:
        ax = fig.add_subplot(m, n, plot_num)
        ...plotting functions...

It is easy to work around using plt.sca(ax), and then the axes keyword in contourf is redundant. But as the keyword is there, I'd expect it to direct the whole function.

We should allow passing an axes to the _label function so that it draws on that if provided. Not doing so was an oversight when support for the axes argument to plotting functions was added in #1942 .

I have created a PR to address this -> #3010

This PR makes sure that the axes kwarg is passed onto the labelling functions as well as the plotting function. I've done this for contourf and the other seven plotting functions in iris.quickplot

Was this page helpful?
0 / 5 - 0 ratings

Related issues

trexfeathers picture trexfeathers  Â·  5Comments

DPeterK picture DPeterK  Â·  5Comments

nhsavage picture nhsavage  Â·  5Comments

jupalm picture jupalm  Â·  3Comments

lbdreyer picture lbdreyer  Â·  3Comments