0

Does anyone know how (if?) I can control the vmin and vmax of an Iris quickplot pcolormesh animation using iris.experimental.animate.animate?

wind = iris.load_cube('/my/pp/file')
cube_iter = wind.slices(('longitude', 'latitude'))
ani = animate(cube_iter, qplt.contourf,vmin=0,vmax=20)
plt.show()

I'm using Iris version 2.4.0.

In this code, the animation works, but vmax is ignored (see attached screenshot).

Does anyone know how to fix this?

Thanks!

Image illustrating that vmax is not being obeyed.

jonnyhtw
  • 61
  • 8
  • 1
    I did a little digging, and I think this might ultimately be an instance of https://stackoverflow.com/questions/50823771/colorbar-does-not-apply-vmin-and-vmax?rq=1. Though the fix would need to be in Iris rather than your code. Feel free to open an issue and/or pull request over on GitHub. – RuthC May 24 '22 at 09:17

1 Answers1

0

This is a bit of a fudge, but we can produce a more sensible animation by using the functools.partial function to fix the levels and extend keyword.

import functools

import iris
from iris.experimental.animate import animate
import iris.quickplot as qplt
import matplotlib.pyplot as plt

my_contourf = functools.partial(qplt.contourf, levels=range(260, 291, 5),
                                extend="both")
# Function defined by partial has no __module__ or __name__ attribute, so add
# them to get past iris animate's checks.
my_contourf.__module__ = "iris.quickplot"
my_contourf.__name__ = "contourf"

fname = iris.sample_data_path("E1_north_america.nc")
cube = iris.load_cube(fname)[:20]

cube_iter = cube.slices(('longitude', 'latitude'))
ani = animate(cube_iter, my_contourf, vmin=260, vmax=290)
ani.save("abc.gif")

enter image description here

RuthC
  • 1,269
  • 1
  • 10
  • 21
  • thanks a lot for this, brilliant! the gif is saved and animates as shown. one thing though, i am using jupyter and this doesn't animate inside the notebook. does this work for you? thanks again! – jonnyhtw Jun 13 '22 at 02:06
  • It works in a notebook for me if I put your `%matplotlib notebook` at the start. – RuthC Jun 14 '22 at 10:15
  • perfect! works now that you pointed this out haha! thanks :) – jonnyhtw Jun 16 '22 at 04:49