@@ -562,6 +562,7 @@ def plot_on_figure(
562562 clamp_to_surface : bool = True ,
563563 bathymetry : xarray .DataArray | None = None ,
564564 cmap : str | Colormap = 'jet' ,
565+ clim : tuple [float , float ] | None = None ,
565566 ocean_floor_colour : str = 'black' ,
566567 landmarks : list [Landmark ] | None = None ,
567568 ) -> None :
@@ -607,6 +608,7 @@ def plot_on_figure(
607608 clamp_to_surface = clamp_to_surface ,
608609 bathymetry = bathymetry ,
609610 cmap = cmap ,
611+ clim = clim ,
610612 ocean_floor_colour = ocean_floor_colour ,
611613 landmarks = landmarks ,
612614 )
@@ -622,6 +624,7 @@ def animate_on_figure(
622624 clamp_to_surface : bool = True ,
623625 bathymetry : xarray .DataArray | None = None ,
624626 cmap : str | Colormap = 'jet' ,
627+ clim : tuple [float , float ] | None = None ,
625628 ocean_floor_colour : str = 'black' ,
626629 landmarks : list [Landmark ] | None = None ,
627630 coordinate : xarray .DataArray | None = None ,
@@ -652,6 +655,7 @@ def animate_on_figure(
652655 if coordinate is None :
653656 coordinate = self .convention .time_coordinate
654657 coordinate_indexes = numpy .arange (coordinate .size )
658+ animation_dimension = coordinate .dims [0 ]
655659
656660 coordinate_callable : Callable [[Any ], str ]
657661 if title is None :
@@ -667,14 +671,17 @@ def animate_on_figure(
667671 else :
668672 coordinate_callable = title
669673
670- axes , collection , data_array = self ._plot_on_figure (
674+ first_frame = data_array .isel ({animation_dimension : 0 })
675+ first_frame .load ()
676+ axes , collection , _prepared_frame = self ._plot_on_figure (
671677 figure = figure ,
672- data_array = data_array ,
678+ data_array = first_frame ,
673679 title = None ,
674680 trim_nans = trim_nans ,
675681 clamp_to_surface = clamp_to_surface ,
676682 bathymetry = bathymetry ,
677683 cmap = cmap ,
684+ clim = clim ,
678685 ocean_floor_colour = ocean_floor_colour ,
679686 landmarks = landmarks ,
680687 )
@@ -686,7 +693,10 @@ def animate(index: int) -> Iterable[Artist]:
686693 axes .set_title (coordinate_callable (coordinate_value ))
687694 changes .append (axes )
688695
689- collection .set_array (data_array [index ].values .flatten ())
696+ frame_data = data_array .isel ({animation_dimension : index })
697+ frame_data .load ()
698+ prepared_data = self .prepare_data_array_for_transect (frame_data )
699+ collection .set_array (prepared_data .values .flatten ())
690700 changes .append (collection )
691701 return changes
692702
@@ -711,6 +721,7 @@ def _plot_on_figure(
711721 clamp_to_surface : bool = True ,
712722 bathymetry : xarray .DataArray | None = None ,
713723 cmap : str | Colormap = 'jet' ,
724+ clim : tuple [float , float ] | None = None ,
714725 ocean_floor_colour : str = 'black' ,
715726 landmarks : list [Landmark ] | None = None ,
716727 ) -> tuple [Axes , PolyCollection , xarray .DataArray ]:
@@ -724,6 +735,7 @@ def _plot_on_figure(
724735 depth = transect_dataset .coords ['depth' ]
725736 distance_bounds = transect_dataset .data_vars ['distance_bounds' ]
726737
738+ data_array = data_array .load ()
727739 data_array = self .prepare_data_array_for_transect (data_array )
728740
729741 positive_down = depth .attrs ['positive' ] == 'down'
@@ -769,12 +781,11 @@ def _plot_on_figure(
769781 cmap = colormaps [cmap ].copy ()
770782 cmap .set_bad (ocean_floor_colour )
771783
772- if data_array .size != 0 :
784+ # Find a min/max from the data if clim isn't provided and the data array is not empty.
785+ # An empty data array happens when the transect line does not intersect
786+ # the dataset geometry.
787+ if clim is None and data_array .size != 0 :
773788 clim = (numpy .nanmin (data_array ), numpy .nanmax (data_array ))
774- else :
775- # An empty data array happens when the transect line does not
776- # intersect the dataset geometry.
777- clim = None
778789
779790 collection = self .make_poly_collection (cmap = cmap , clim = clim , edgecolor = 'face' )
780791 axes .add_collection (collection )
0 commit comments