Time axis on a contourf plot

1,147 views
Skip to first unread message

LeonH

unread,
Jul 18, 2013, 4:59:03 AM7/18/13
to scitoo...@googlegroups.com
Hi All

iris.quickplot.plot() handles dates admirably when one axis of the plot is a time axis. However, when I use iris.quickplot.contourf(cube, coords=['time', 'latitude']) I get the raw numbers from the time axis and not dates. Does anyone know how to fix this?

Thanks
Leon
 

Andrew Dawson

unread,
Jul 18, 2013, 5:18:27 AM7/18/13
to scitoo...@googlegroups.com
Hi Leon

This is a little complicated actually. Iris will not do the date conversion here to avoid a limitation in matplotlib. A fix for the issue in matplotlib is currently under review.

The good news is that even though matplotlib has some issues with datetime coordinates for 2d plots, contourf in *matplotlib* can handle 1D datetime coordinates. The bad news is that Iris won't let you...

At the moment I can only offer you the extremely crude workaround of doing it manually. Assuming the cube is an iris cube ['time', 'latitude']:

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

cube
= ...   # some cube loading here

# get the cube's coordinates
time
= cube.coord('time')
lat
= cube.coord('latitude')

# construct nice datetime values from the time coordinate
# borrows a private function from iris.plot, not a great idea
# in general but it works for now
x
= iplt._fixup_dates(time, time.points)

# extract latitude coordinate points and data values
y
= lat.points
z
= cube.data.T

plt
.contourf(x, y, z)
plt
.gcf().autofmt_xdate()# makes labels easier to read
# you'll need to add your own axis labels and title
# ...
plt
.show()

It would be really good if we could implement a temporary fix for this in iris, since the matplotlib fix won't appear until mpl v1.4.0 which is around 6 months away I would imagine. I'm not sure how though. Perhaps others have some ideas...

Andrew

Andrew Dawson

unread,
Jul 18, 2013, 5:52:39 AM7/18/13
to scitoo...@googlegroups.com
I forgot to mention that the same workaround will *not* work for pcolor or pcolormesh. This is due to the matplotlib issues mentioned before.

LeonH

unread,
Jul 18, 2013, 8:15:10 AM7/18/13
to scitoo...@googlegroups.com
it does work for my case (contourf), so thank you very much, Andrew!

Leon

Niall Robinson

unread,
Mar 10, 2014, 11:03:30 AM3/10/14
to scitoo...@googlegroups.com
Hi - Does anyone know if this has been fixed yet? I've just got the same problem

Andrew Dawson

unread,
Mar 10, 2014, 11:05:06 AM3/10/14
to scitoo...@googlegroups.com
The fix is in matplotlib 1.4.x, but since that isn't released yet Iris has not modified its behaviour in this regard.

Niall Robinson

unread,
Mar 10, 2014, 11:24:19 AM3/10/14
to scitoo...@googlegroups.com
Thanks Andrew

DeeKay

unread,
Mar 14, 2014, 9:09:52 AM3/14/14
to scitoo...@googlegroups.com
For clarification, as of Iris v1.6.1+ (and the required matplotlib 1.3.1+) it is possible to plot nice dates on the time axis of a contour plot, but not a pseudocolor plot due to differences in the underlying matplotlib implementation. You may well need to specify (assuming your x axis is the time axis):

plt.gca().xaxis.axis_date()

in order to actually get some nice date formatting. To gain further control of the actual formatting of the date ticks, you can use:

plt.gca().xaxis.set_major_formatter()

passing a matplotlib.dates.DateFormatter object to set_major_formatter to specify the date format.

Andrew Dawson

unread,
Mar 14, 2014, 9:51:10 AM3/14/14
to scitoo...@googlegroups.com
This is pretty much exactly what I said in my response to the original question. I think the example I gave still reflects the current status of this issue, nothing has changed in matplotlib or iris since then that would change this. My matplotlib fix for this was always for version 1.4, it was too late for the 1.3 release.
 
In that example I used plt.gcf().autofmt_xdate() which is a nice easy way to get the formatting right.

DeeKay

unread,
Mar 14, 2014, 11:59:44 AM3/14/14
to scitoo...@googlegroups.com
I'd no intention of undercutting your earlier example! I was just pointing out (seeing as the issue had re-appeared) that iris.plot.contour(f) now do deal better with dates than was the case when the original question was asked. I included the two gca().xaxis lines for completeness and the fact that they are still required for date formatting to work well, even without the addition to iris.plot...

Jonny Williams

unread,
Sep 20, 2017, 10:42:11 PM9/20/17
to Iris
Just to say that this thread has helped me greatly! Cheeeeeeers

Jonny Williams

NIWA, NZ

Phil Elson

unread,
Sep 21, 2017, 4:38:28 AM9/21/17
to Jonny Williams, Iris
Just to update the thread then, you might find utility in nc-time-axis, which has been designed to visualise these datetimes in matplotlib: https://github.com/SciTools/nc-time-axis

--
You received this message because you are subscribed to the Google Groups "Iris" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scitools-iris+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages