Skip to content

pcolormesh / _draw_2d_from_bounds doesn't handle time coordinates #3347

@rcomer

Description

@rcomer

This was raised via Met Office Yammer. Given a 2d cube with a time dimension, pcolormesh does not recognise the time coordinate as such. Example:

import iris
import iris.plot as iplt
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

time_coord = iris.coords.DimCoord(range(12), standard_name='time',
                                  units='days since 2019-06-01')

height_coord = iris.coords.DimCoord(range(5), standard_name='height', units='m')

cube = iris.cube.Cube(np.arange(12) + np.arange(5)[:, np.newaxis])
for dim, coord in enumerate([height_coord, time_coord]):
    cube.add_dim_coord(coord, dim)

iplt.contourf(cube)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d %b'))
plt.show()

iplt.pcolormesh(cube)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d %b'))
plt.show()

contourf produces this:
tcoord_contour

For the pcolormesh plot, matplotlib thows an error about non datetime values. Without the set_major_formatter call, we get this:
pcolormesh_eg

contourf uses _draw_2d_from_points which has specific handling for time coordinates. pcolormesh uses _draw_2d_from_bounds, which doesn't have this handling (as far as I can see). So possibly we just need to add that if loop to make it consistent (I haven't tried).

There seems to be a fair bit of overlap between the code of _draw_2d_from_points and _draw_2d_from_bounds, so I wonder if it's worth doing some rationalisation while addressing this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions