-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDEAPlotting.py
1774 lines (1350 loc) · 85.4 KB
/
DEAPlotting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# DEAPlotting.py
"""
This file contains a set of python functions for plotting DEA data.
Available functions:
rgb
animated_timeseries
animated_timeseriesline
animated_doubletimeseries
plot_WOfS
display_map
three_band_image (depreciated)
three_band_image_subplots (depreciated)
Last modified: March 2019
Authors: Claire Krause, Robbi Bishop-Taylor, Sean Chua, Mike Barnes, Cate Kooymans, Bex Dunn
"""
# Load modules
import warnings
import numpy as np
import pandas as pd
from skimage import exposure
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.patheffects as PathEffects
from datetime import datetime
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import calendar
import geopandas as gpd
from matplotlib.colors import ListedColormap
from mpl_toolkits.axes_grid1 import make_axes_locatable
import folium
import itertools
import math
from pyproj import Proj, transform
def rgb(ds, bands=['red', 'green', 'blue'], index=None, index_dim='time',
robust=True, percentile_stretch = None, col_wrap=4, size=6, aspect=1,
savefig_path=None, savefig_kwargs={}, **kwargs):
"""
Takes an xarray dataset and plots RGB images using three imagery bands (e.g true colour ['red', 'green', 'blue']
or false colour ['swir1', 'nir', 'green']). The `index` parameter allows easily selecting individual or multiple
images for RGB plotting. Images can be saved to file by specifying an output path using `savefig_path`.
This function was designed to work as an easy-to-use wrapper around xarray's `.plot.imshow()` functionality.
Last modified: November 2018
Author: Robbi Bishop-Taylor
Parameters
----------
ds : xarray Dataset
A two-dimensional or multi-dimensional array to plot as an RGB image. If the array has more than two
dimensions (e.g. multiple observations along a 'time' dimension), either use `index` to select one (`index=0`)
or multiple observations (`index=[0, 1]`), or create a custom faceted plot using e.g. `col="time", col_wrap=4`.
bands : list of strings, optional
A list of three strings giving the band names to plot. Defaults to '['red', 'green', 'blue']'.
index : integer or list of integers, optional
For convenience `index` can be used to select one (`index=0`) or multiple observations (`index=[0, 1]`) from
the input dataset for plotting. If multiple images are requested these will be plotted as a faceted plot.
index_dim : string, optional
The dimension along which observations should be plotted if multiple observations are requested using `index`.
Defaults to `time`.
robust : bool, optional
Produces an enhanced image where the colormap range is computed with 2nd and 98th percentiles instead of the
extreme values. Defaults to True.
percentile_stretch : tuple of floats
An tuple of two floats (between 0.00 and 1.00) that can be used to clip the colormap range to manually
specified percentiles to get more control over the brightness and contrast of the image. The default is None;
'(0.02, 0.98)' is equivelent to `robust=True`. If this parameter is used, `robust` will have no effect.
col_wrap : integer, optional
The maximum number of columns allowed in faceted plots. Defaults to 4.
size : integer, optional
The height (in inches) of each plot. Defaults to 6.
aspect : integer, optional
Aspect ratio of each facet in the plot, so that aspect * size gives width of each facet in inches. Defaults to 1.
savefig_path : string, optional
Path to export image file for the RGB plot. Defaults to None, which does not export an image file.
savefig_kwargs : dict, optional
A dict of keyword arguments to pass to `matplotlib.pyplot.savefig` when exporting an image file. For options,
see: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.savefig.html
**kwargs : optional
Additional keyword arguments to pass to `xarray.plot.imshow()`. For more options, see:
http://xarray.pydata.org/en/stable/generated/xarray.plot.imshow.html
Returns
-------
An RGB plot of one or multiple observations, and optionally an image file written to file.
Example
-------
>>> # Import modules
>>> import sys
>>> import datacube
>>> # Import external dea-notebooks functions using relative link to Scripts directory
>>> sys.path.append('../10_Scripts')
>>> import DEAPlotting
>>> # Set up datacube instance
>>> dc = datacube.Datacube(app='RGB plotting')
>>> # Define a Landsat query
>>> landsat_query = {'lat': (-35.25, -35.35),
... 'lon': (149.05, 149.17),
... 'time': ('2016-02-15', '2016-03-01'),
... 'output_crs': 'EPSG:3577',
... 'resolution': (-25, 25)}
>>> # Import sample Landsat data
>>> landsat_data = dc.load(product='ls8_nbart_albers',
... group_by='solar_day',
... **landsat_query)
>>> # Plot a single observation (option 1)
>>> DEAPlotting.rgb(ds=landsat_data.isel(time=0))
>>> # Plot a single observation using `index` (option 2)
>>> DEAPlotting.rgb(ds=landsat_data, index=0)
>>> # Plot multiple observations as a facet plot (option 1)
>>> DEAPlotting.rgb(ds=landsat_data, col='time')
>>> # Plot multiple observations as a facet plot using `index` (option 2)
>>> DEAPlotting.rgb(ds=landsat_data, index=[0, 1])
>>> # Increase contrast by specifying percentile thresholds using `percentile_stretch`
>>> DEAPlotting.rgb(ds=landsat_data, index=[0, 1],
... percentile_stretch=(0.02, 0.9))
>>> # Pass in any keyword argument to `xarray.plot.imshow()` (e.g. `aspect`). For more
>>> # options, see: http://xarray.pydata.org/en/stable/generated/xarray.plot.imshow.html
>>> DEAPlotting.rgb(ds=landsat_data, index=[0, 1],
... percentile_stretch=(0.02, 0.9), aspect=1.2)
>>> # Export the RGB image to file using `savefig_path`
>>> DEAPlotting.rgb(ds=landsat_data, index=[0, 1],
... percentile_stretch=(0.02, 0.9), aspect=1.2,
... savefig_path='output_image_test.png')
Exporting image to output_image_test.png
"""
# If no value is supplied for `index` (the default), plot using default values and arguments passed via `**kwargs`
if index is None:
if len(ds.dims) > 2 and 'col' not in kwargs:
raise Exception(f'The input dataset `ds` has more than two dimensions: {list(ds.dims.keys())}. '
'Please select a single observation using e.g. `index=0`, or enable faceted '
'plotting by adding the arguments e.g. `col="time", col_wrap=4` to the function call')
# Select bands and convert to DataArray
da = ds[bands].to_array()
# If percentile_stretch is provided, clip plotting to percentile vmin, vmax
if percentile_stretch:
vmin, vmax = da.quantile(percentile_stretch).values
kwargs.update({'vmin': vmin, 'vmax': vmax})
img = da.plot.imshow(robust=robust, col_wrap=col_wrap, size=size, **kwargs)
# If values provided for `index`, extract corresponding observations and plot as either single image or facet plot
else:
# If a float is supplied instead of an integer index, raise exception
if isinstance(index, float):
raise Exception(f'Please supply `index` as either an integer or a list of integers')
# If col argument is supplied as well as `index`, raise exception
if 'col' in kwargs:
raise Exception(f'Cannot supply both `index` and `col`; please remove one and try again')
# Convert index to generic type list so that number of indices supplied can be computed
index = index if isinstance(index, list) else [index]
# Select bands and observations and convert to DataArray
da = ds[bands].isel(**{index_dim: index}).to_array()
# If percentile_stretch is provided, clip plotting to percentile vmin, vmax
if percentile_stretch:
vmin, vmax = da.quantile(percentile_stretch).values
kwargs.update({'vmin': vmin, 'vmax': vmax})
# If multiple index values are supplied, plot as a faceted plot
if len(index) > 1:
img = da.plot.imshow(robust=robust, col=index_dim, col_wrap=col_wrap, size=size, **kwargs)
# If only one index is supplied, squeeze out index_dim and plot as a single panel
else:
img = da.squeeze(dim=index_dim).plot.imshow(robust=robust, size=size, **kwargs)
# If an export path is provided, save image to file. Individual and faceted plots have a different API (figure
# vs fig) so we get around this using a try statement:
if savefig_path:
print(f'Exporting image to {savefig_path}')
try:
img.fig.savefig(savefig_path, **savefig_kwargs)
except:
img.figure.savefig(savefig_path, **savefig_kwargs)
def animated_timeseries(ds, output_path,
width_pixels=600, interval=200, bands=['red', 'green', 'blue'],
percentile_stretch = (0.02, 0.98), image_proc_func=None,
title=False, show_date=True, annotation_kwargs={},
onebandplot_cbar=True, onebandplot_kwargs={},
shapefile_path=None, shapefile_kwargs={},
time_dim = 'time', x_dim = 'x', y_dim = 'y'):
"""
Takes an xarray time series and animates the data as either a three-band (e.g. true or false colour)
or single-band animation, allowing changes in the landscape to be compared across time.
Animations can be exported as .mp4 (ideal for Twitter/social media), .wmv (ideal for Powerpoint) and .gif
(ideal for all purposes, but can have large file sizes) format files, and customised to include titles and
date annotations or use specific combinations of input bands.
A shapefile boundary can be added to the output animation by providing a path to the shapefile.
This function can be used to produce visually appealing cloud-free animations when used in combination with
the `load_clearlandsat` function from `dea-notebooks/10_Scripts/DEADataHandling`.
Last modified: October 2018
Author: Robbi Bishop-Taylor, Sean Chua, Bex Dunn
:param ds:
An xarray dataset with multiple time steps (i.e. multiple observations along the `time` dimension).
:param output_path:
A string giving the output location and filename of the resulting animation. File extensions of '.mp4',
'.wmv' and '.gif' are accepted.
:param width_pixels:
An integer defining the output width in pixels for the resulting animation. The height of the animation is
set automatically based on the dimensions/ratio of the input xarray dataset. Defaults to 600 pixels wide.
:param interval:
An integer defining the milliseconds between each animation frame used to control the speed of the output
animation. Higher values result in a slower animation. Defaults to 200 milliseconds between each frame.
:param bands:
An optional list of either one or three bands to be plotted, all of which must exist in `ds`.
Defaults to `['red', 'green', 'blue']`.
:param percentile_stretch:
An optional tuple of two floats that can be used to clip one or three-band arrays by percentiles to produce
a more vibrant, visually attractive image that is not affected by outliers/extreme values. The default is
`(0.02, 0.98)` which is equivalent to xarray's `robust=True`.
:param image_proc_func:
An optional function can be passed to modify three-band arrays for each timestep prior to animating.
This could include image processing functions such as increasing contrast, unsharp masking, saturation etc.
The function should take AND return a three-band numpy array with shape [:, :, 3]. If your function has
parameters, you can pass in custom values using `partial` from `functools`:
`image_proc_func=partial(custom_func, param1=10)`.
:param title:
An optional string or list of strings with a length equal to the number of timesteps in ds. This can be
used to display a static title (using a string), or a dynamic title (using a list) that displays different
text for each timestep. Defaults to False, which plots no title.
:param show_date:
An optional boolean that defines whether or not to plot date annotations for each animation frame. Defaults
to True, which plots date annotations based on ds.
:param annotation_kwargs:
An optional dict of kwargs for controlling the appearance of text annotations to pass to the matplotlib
`plt.annotate` function (see https://matplotlib.org/api/_as_gen/matplotlib.pyplot.annotate.html for options).
For example, `annotation_kwargs={'fontsize':20, 'color':'red', 'family':'serif'}. By default, text annotations
are plotted as white, size 25 mono-spaced font with a 4pt black outline in the top-right of the animation.
:param onebandplot_cbar:
An optional boolean indicating whether to include a colourbar for `ds1` one-band arrays. Defaults to True.
:param onebandplot_kwargs:
An optional dict of kwargs for controlling the appearance of one-band image arrays to pass to matplotlib
`plt.imshow` (see https://matplotlib.org/api/_as_gen/matplotlib.pyplot.imshow.html for options).
This only applies if an xarray with a single band is passed to `ds`. For example, a green colour scheme and
custom stretch could be specified using: `onebandplot_kwargs={'cmap':'Greens`, 'vmin':0.2, 'vmax':0.9}`.
By default, one-band arrays are plotted using the 'Greys' cmap with bilinear interpolation.
Two special kwargs (`tick_fontsize`, `tick_colour`) can also be passed to control the tick labels on the
colourbar. This can be useful for example when the tick labels are difficult to see against a dark background.
:param shapefile_path:
An optional string or list of strings giving the file paths of one or multiple shapefiles to overlay on the
output animation. The shapefiles must be in the same projection as the input xarray dataset.
:param shapefile_kwargs:
An optional dictionary of kwargs or list of dictionaries to specify the appearance of the shapefile overlay
by passing to `GeoSeries.plot` (see http://geopandas.org/reference.html#geopandas.GeoSeries.plot). For example:
`shapefile_kwargs = {'linewidth':2, 'edgecolor':'black', 'facecolor':"#00000000"}`. If multiple shapefiles
were provided to `shapefile_path`, each shapefile can be plotted with a different colour style by passing in
a list of kwarg dicts of the same length as `shapefile_path`.
:param time_dim:
An optional string allowing you to override the xarray dimension used for time. Defaults to 'time'.
:param x_dim:
An optional string allowing you to override the xarray dimension used for x coordinates. Defaults to 'x'.
:param y_dim:
An optional string allowing you to override the xarray dimension used for y coordinates. Defaults to 'y'.
"""
###############
# Setup steps #
###############
# Test if all dimensions exist in dataset
if time_dim in ds and x_dim in ds and y_dim in ds:
# First test if there are three bands, and that all exist in both datasets:
if ((len(bands) == 3) | (len(bands) == 1)) & all([(b in ds.data_vars) for b in bands]):
# Import xarrays as lists of three band numpy arrays
imagelist, vmin, vmax = _ds_to_arrraylist(ds, bands=bands,
time_dim=time_dim, x_dim=x_dim, y_dim=y_dim,
percentile_stretch=percentile_stretch,
image_proc_func=image_proc_func)
# Get time, x and y dimensions of dataset and calculate width vs height of plot
timesteps = len(ds[time_dim])
width = len(ds[x_dim])
height = len(ds[y_dim])
width_ratio = float(width) / float(height)
height = 10.0 / width_ratio
# If title is supplied as a string, multiply out to a list with one string per timestep.
# Otherwise, use supplied list for plot titles.
if isinstance(title, str) or isinstance(title, bool):
title_list = [title] * timesteps
else:
title_list = title
# Set up annotation parameters that plt.imshow plotting for single band array images.
# The nested dict structure sets default values which can be overwritten/customised by the
# manually specified `onebandplot_kwargs`
onebandplot_kwargs = dict({'cmap': 'Greys', 'interpolation': 'bilinear',
'vmin': vmin, 'vmax': vmax, 'tick_colour': 'black', 'tick_fontsize': 15},
**onebandplot_kwargs)
# Use pop to remove the two special tick kwargs from the onebandplot_kwargs dict, and save individually
onebandplot_tick_colour = onebandplot_kwargs.pop('tick_colour')
onebandplot_tick_fontsize = onebandplot_kwargs.pop('tick_fontsize')
# Set up annotation parameters that control font etc. The nested dict structure sets default
# values which can be overwritten/customised by the manually specified `annotation_kwargs`
annotation_kwargs = dict({'xy': (1, 1), 'xycoords': 'axes fraction',
'xytext': (-5, -5), 'textcoords': 'offset points',
'horizontalalignment': 'right', 'verticalalignment':'top',
'fontsize': 28, 'color': 'white',
'path_effects': [PathEffects.withStroke(linewidth=3, foreground='black')]},
**annotation_kwargs)
###################
# Initialise plot #
###################
# Set up figure
fig, ax1 = plt.subplots(ncols=1)
fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
fig.set_size_inches(10.0, height, forward=True)
ax1.axis('off')
# Initialise axesimage objects to be updated during animation, setting extent from dims
extents = [float(ds[x_dim].min()), float(ds[x_dim].max()),
float(ds[y_dim].min()), float(ds[y_dim].max())]
im = ax1.imshow(imagelist[0], extent=extents, **onebandplot_kwargs)
# Initialise annotation objects to be updated during animation
t = ax1.annotate('', **annotation_kwargs)
#########################
# Add optional overlays #
#########################
# Optionally add shapefile overlay(s) from either string path or list of string paths
if isinstance(shapefile_path, str):
# Define default plotting parameters for the overlaying shapefile(s). The nested dict structure sets
# default values which can be overwritten/customised by the manually specified `shapefile_kwargs`
shapefile_kwargs = dict({'linewidth': 2, 'edgecolor': 'black', 'facecolor': "#00000000"},
**shapefile_kwargs)
shapefile = gpd.read_file(shapefile_path)
shapefile.plot(**shapefile_kwargs, ax=ax1)
elif isinstance(shapefile_path, list):
# Iterate through list of string paths
for i, shapefile in enumerate(shapefile_path):
if isinstance(shapefile_kwargs, list):
# If a list of shapefile_kwargs is supplied, use one for each shapefile
shapefile_kwargs_i = dict({'linewidth': 2, 'edgecolor': 'black', 'facecolor': "#00000000"},
**shapefile_kwargs[i])
shapefile = gpd.read_file(shapefile)
shapefile.plot(**shapefile_kwargs_i, ax=ax1)
else:
# If one shapefile_kwargs is provided, use for all shapefiles
shapefile_kwargs = dict({'linewidth': 2, 'edgecolor': 'black', 'facecolor': "#00000000"},
**shapefile_kwargs)
shapefile = gpd.read_file(shapefile)
shapefile.plot(**shapefile_kwargs, ax=ax1)
# After adding shapefile, fix extents of plot
ax1.set_xlim(extents[0], extents[1])
ax1.set_ylim(extents[2], extents[3])
# Optionally add colourbar for one band images
if (len(bands) == 1) & onebandplot_cbar:
_add_colourbar(ax1, im,
tick_fontsize=onebandplot_tick_fontsize,
tick_colour=onebandplot_tick_colour,
vmin=onebandplot_kwargs['vmin'],
vmax=onebandplot_kwargs['vmax'],
cmap=onebandplot_kwargs['cmap'])
########################################
# Create function to update each frame #
########################################
# Function to update figure
def update_figure(frame_i):
# If possible, extract dates from time dimension
try:
# Get human-readable date info (e.g. "16 May 1990")
ts = ds[time_dim][{time_dim:frame_i}].dt
year = ts.year.item()
month = ts.month.item()
day = ts.day.item()
date_string = '{} {} {}'.format(day, calendar.month_abbr[month], year)
except:
date_string = ds[time_dim][{time_dim:frame_i}].values.item()
# Create annotation string based on title and date specifications:
title = title_list[frame_i]
if title and show_date:
title_date = '{}\n{}'.format(date_string, title)
elif title and not show_date:
title_date = '{}'.format(title)
elif show_date and not title:
title_date = '{}'.format(date_string)
else:
title_date = ''
# Update figure for frame
im.set_array(imagelist[frame_i])
t.set_text(title_date)
# Return the artists set
return [im, t]
##############################
# Generate and run animation #
##############################
# Generate animation
print('Generating {} frame animation'.format(timesteps))
ani = animation.FuncAnimation(fig, update_figure, frames=timesteps, interval=interval, blit=True)
# Export as either MP4 or GIF
if output_path[-3:] == 'mp4':
print(' Exporting animation to {}'.format(output_path))
ani.save(output_path, dpi=width_pixels / 10.0)
elif output_path[-3:] == 'wmv':
print(' Exporting animation to {}'.format(output_path))
ani.save(output_path, dpi=width_pixels / 10.0,
writer=animation.FFMpegFileWriter(fps=1000 / interval, bitrate=4000, codec='wmv2'))
elif output_path[-3:] == 'gif':
print(' Exporting animation to {}'.format(output_path))
ani.save(output_path, dpi=width_pixels / 10.0, writer='imagemagick')
else:
print(' Output file type must be either .mp4, .wmv or .gif')
else:
print('Please select either one or three bands that all exist in the input dataset')
else:
print('At least one x, y or time dimension does not exist in the input dataset. Please use the `time_dim`,' \
'`x_dim` or `y_dim` parameters to override the default dimension names used for plotting')
def animated_timeseriesline(ds, df, output_path,
width_pixels=1000, interval=200, bands=['red', 'green', 'blue'],
percentile_stretch = (0.02, 0.98), image_proc_func=None,
title=False, show_date=True, annotation_kwargs={},
onebandplot_cbar=True, onebandplot_kwargs={},
shapefile_path=None, shapefile_kwargs={}, pandasplot_kwargs={},
time_dim = 'time', x_dim = 'x', y_dim = 'y'):
"""
Takes an xarray time series and a pandas dataframe, and animates a line graph showing change in a variable
across time in the right column at the same time as a three-band (e.g. true or false colour) or single-band
animation in the left column.
Animations can be exported as .mp4 (ideal for Twitter/social media), .wmv (ideal for Powerpoint) and .gif
(ideal for all purposes, but can have large file sizes) format files, and customised to include titles and
date annotations or use specific combinations of input bands.
A shapefile boundary can be added to the output animation by providing a path to the shapefile.
This function can be used to produce visually appealing cloud-free animations when used in combination with
the `load_clearlandsat` function from `dea-notebooks/10_Scripts/DEADataHandling`.
Last modified: October 2018
Author: Robbi Bishop-Taylor, Sean Chua, Bex Dunn
:param ds:
An xarray dataset with multiple time steps (i.e. multiple observations along the `time` dimension) to plot
in the left panel of the animation.
:param df:
An pandas dataframe with time steps contained in a DatetimeIndex column, and one or more numeric data
columns to plot as lines in the right panel. Column names are used to label the lines on the plot, so
assign them informative names. Lines are plotted by showing all parts of the line with dates on or before
the current timestep (i.e. for a 2006 time step, only the portion of the lines with dates on or before
2006 will be plotted for that frame.
:param output_path:
A string giving the output location and filename of the resulting animation. File extensions of '.mp4',
'.wmv' and '.gif' are accepted.
:param width_pixels:
An integer defining the output width in pixels for the resulting animation. The height of the animation is
set automatically based on the dimensions/ratio of the input xarray dataset. Defaults to 1000 pixels wide.
:param interval:
An integer defining the milliseconds between each animation frame used to control the speed of the output
animation. Higher values result in a slower animation. Defaults to 200 milliseconds between each frame.
:param bands:
An optional list of either one or three bands to be plotted in the left panel, all of which must exist in
`ds`. Defaults to `['red', 'green', 'blue']`.
:param percentile_stretch:
An optional tuple of two floats that can be used to clip one or three-band arrays in the left panel by
percentiles to produce a more vibrant, visually attractive image that is not affected by outliers/extreme
values. The default is `(0.02, 0.98)` which is equivalent to xarray's `robust=True`.
:param image_proc_func:
An optional function can be passed to modify three-band arrays for each timestep prior to animating.
This could include image processing functions such as increasing contrast, unsharp masking, saturation etc.
The function should take AND return a three-band numpy array with shape [:, :, 3]. If your function has
parameters, you can pass in custom values using `partial` from `functools`:
`image_proc_func=partial(custom_func, param1=10)`.
:param title:
An optional string or list of strings with a length equal to the number of timesteps in `ds`. This can be
used to display a static title (using a string), or a dynamic title (using a list) that displays different
text for each timestep. Defaults to False, which plots no title.
:param show_date:
An optional boolean that defines whether or not to plot date annotations for each animation frame. Defaults
to True, which plots date annotations based on time steps in `ds`.
:param annotation_kwargs:
An optional dict of kwargs for controlling the appearance of text annotations in the left panel to pass to the
matplotlib `plt.annotate` function (see https://matplotlib.org/api/_as_gen/matplotlib.pyplot.annotate.html).
For example, `annotation_kwargs={'fontsize':20, 'color':'red', 'family':'serif'}. By default, text annotations
are plotted as white, size 25 mono-spaced font with a 4pt black outline in the top-right of the animation.
:param onebandplot_cbar:
An optional boolean indicating whether to include a colourbar if `ds` is a one-band array. Defaults to True.
:param onebandplot_kwargs:
An optional dict of kwargs for controlling the appearance of one-band image arrays in the left panel to pass
to matplotlib `plt.imshow` (see https://matplotlib.org/api/_as_gen/matplotlib.pyplot.imshow.html for options).
This only applies if an xarray with a single band is passed to `ds`. For example, a green colour scheme and
custom stretch could be specified using: `onebandplot_kwargs={'cmap':'Greens`, 'vmin':0.2, 'vmax':0.9}`.
By default, one-band arrays are plotted using the 'Greys' cmap with bilinear interpolation.
Two special kwargs (`tick_fontsize`, `tick_colour`) can also be passed to control the tick labels on the
colourbar. This can be useful for example when the tick labels are difficult to see against a dark background.
:param shapefile_path:
An optional string or list of strings giving the file paths of shapefiles to overlay on the output animation.
The shapefiles must be in the same projection as the input xarray dataset.
:param shapefile_kwargs:
An optional dict of kwargs to specify the appearance of the shapefile overlay to pass to `GeoSeries.plot`
(see http://geopandas.org/reference.html#geopandas.GeoSeries.plot). For example:
`shapefile_kwargs = {'linewidth':2, 'edgecolor':'black', 'facecolor':"#00000000"}`
:param pandasplot_kwargs:
An optional dict of kwargs to specify the appearance of the right-hand plot to pass to `pandas.DataFrame.plot`
(see https://pandas.pydata.org/pandas-docs/version/0.22/generated/pandas.DataFrame.plot.html). For example:
`pandasplot_kwargs = {'linewidth':2, 'cmap':'viridis', 'ylim':(0, 100)}`
:param time_dim:
An optional string allowing you to override the xarray dimension used for time. Defaults to 'time'.
:param x_dim:
An optional string allowing you to override the xarray dimension used for x coordinates. Defaults to 'x'.
:param y_dim:
An optional string allowing you to override the xarray dimension used for y coordinates. Defaults to 'y'.
"""
###############
# Setup steps #
###############
# Test if all dimensions exist in dataset
if time_dim in ds and x_dim in ds and y_dim in ds:
# Test if there is one or three bands, and that all exist in both datasets:
if ((len(bands) == 3) | (len(bands) == 1)) & all([(b in ds.data_vars) for b in bands]):
# Import xarrays as lists of three band numpy arrays
imagelist, vmin, vmax = _ds_to_arrraylist(ds, bands=bands,
time_dim=time_dim, x_dim=x_dim, y_dim=y_dim,
percentile_stretch=percentile_stretch,
image_proc_func=image_proc_func)
# Get time, x and y dimensions of dataset and calculate width vs height of plot
timesteps = len(ds[time_dim])
width = len(ds[x_dim])
height = len(ds[y_dim])
width_ratio = float(width) / float(height)
height = 10.0 / width_ratio
# If title is supplied as a string, multiply out to a list with one string per timestep.
# Otherwise, use supplied list for plot titles.
if isinstance(title, str) or isinstance(title, bool):
title_list = [title] * timesteps
else:
title_list = title
# Set up annotation parameters that plt.imshow plotting for single band array images.
# The nested dict structure sets default values which can be overwritten/customised by the
# manually specified `onebandplot_kwargs`
onebandplot_kwargs = dict({'cmap':'Greys', 'interpolation':'bilinear',
'vmin': vmin, 'vmax': vmax, 'tick_colour': 'black', 'tick_fontsize': 11},
**onebandplot_kwargs)
# Use pop to remove the two special tick kwargs from the onebandplot_kwargs dict, and save individually
onebandplot_tick_colour = onebandplot_kwargs.pop('tick_colour')
onebandplot_tick_fontsize = onebandplot_kwargs.pop('tick_fontsize')
# Set up annotation parameters that control font etc. The nested dict structure sets default
# values which can be overwritten/customised by the manually specified `annotation_kwargs`
annotation_kwargs = dict({'xy': (1, 1), 'xycoords':'axes fraction',
'xytext':(-5, -5), 'textcoords':'offset points',
'horizontalalignment':'right', 'verticalalignment':'top',
'fontsize':15, 'color':'white',
'path_effects': [PathEffects.withStroke(linewidth=3, foreground='black')]},
**annotation_kwargs)
# Define default plotting parameters for the overlaying shapefile(s). The nested dict structure sets
# default values which can be overwritten/customised by the manually specified `shapefile_kwargs`
shapefile_kwargs = dict({'linewidth': 2, 'edgecolor': 'black', 'facecolor': "#00000000"},
**shapefile_kwargs)
# Define default plotting parameters for the right-hand line plot. The nested dict structure sets
# default values which can be overwritten/customised by the manually specified `pandasplot_kwargs`
pandasplot_kwargs = dict({}, **pandasplot_kwargs)
###################
# Initialise plot #
###################
# Set up figure
fig, (ax1, ax2) = plt.subplots(ncols=2)
fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0.2, hspace=0)
fig.set_size_inches(10.0, height * 0.5, forward=True)
ax1.axis('off')
ax2.margins(x=0.01)
ax2.xaxis.label.set_visible(False)
# Initialise axesimage objects to be updated during animation, setting extent from dims
extents = [float(ds[x_dim].min()), float(ds[x_dim].max()),
float(ds[y_dim].min()), float(ds[y_dim].max())]
im = ax1.imshow(imagelist[0], extent=extents, **onebandplot_kwargs)
# Initialise right panel and set y axis limits
line_test = df.plot(ax=ax2, **pandasplot_kwargs)
# Legend to right panel
ax2.legend(loc='upper left', bbox_to_anchor=(0, 1), ncol=1, frameon=False)
# Initialise annotation objects to be updated during animation
t = ax1.annotate('', **annotation_kwargs)
#########################
# Add optional overlays #
#########################
# Optionally add shapefile overlay(s) from either string path or list of string paths
if isinstance(shapefile_path, str):
shapefile = gpd.read_file(shapefile_path)
shapefile.plot(**shapefile_kwargs, ax=ax1)
elif isinstance(shapefile_path, list):
# Iterate through list of string paths
for shapefile in shapefile_path:
shapefile = gpd.read_file(shapefile)
shapefile.plot(**shapefile_kwargs, ax=ax1)
# After adding shapefile, fix extents of plot
ax1.set_xlim(extents[0], extents[1])
ax1.set_ylim(extents[2], extents[3])
# Optionally add colourbar for one band images
if (len(bands) == 1) & onebandplot_cbar:
_add_colourbar(ax1, im,
tick_fontsize=onebandplot_tick_fontsize,
tick_colour=onebandplot_tick_colour,
vmin=onebandplot_kwargs['vmin'],
vmax=onebandplot_kwargs['vmax'])
########################################
# Create function to update each frame #
########################################
# Function to update figure
def update_figure(frame_i):
####################
# Plot image panel #
####################
# If possible, extract dates from time dimension
try:
# Get human-readable date info (e.g. "16 May 1990")
ts = ds[time_dim][{time_dim:frame_i}].dt
year = ts.year.item()
month = ts.month.item()
day = ts.day.item()
date_string = '{} {} {}'.format(day, calendar.month_abbr[month], year)
except:
date_string = ds[time_dim][{time_dim:frame_i}].values.item()
# Create annotation string based on title and date specifications:
title = title_list[frame_i]
if title and show_date:
title_date = '{}\n{}'.format(date_string, title)
elif title and not show_date:
title_date = '{}'.format(title)
elif show_date and not title:
title_date = '{}'.format(date_string)
else:
title_date = ''
# Update left panel with annotation and image
im.set_array(imagelist[frame_i])
t.set_text(title_date)
########################
# Plot linegraph panel #
########################
# Create list of artists to return
artist_list = [im, t]
# Update right panel with temporal line subset, adding each new line into artist_list
for i, line in enumerate(line_test.lines):
# Clip line data to current time, and get x and y values
y = df[df.index <= datetime(year=year, month=month, day=day, hour=23, minute=59)].iloc[:,i]
x = df[df.index <= datetime(year=year, month=month, day=day, hour=23, minute=59)].index
# Plot lines after stripping NaNs (this produces continuous, unbroken lines)
line.set_data(x[y.notnull()], y[y.notnull()])
artist_list.extend([line])
# Return the artists set
return artist_list
# Nicely space subplots
fig.tight_layout()
##############################
# Generate and run animation #
##############################
# Generate animation
ani = animation.FuncAnimation(fig=fig, func=update_figure, frames=timesteps, interval=interval, blit=True)
# Export as either MP4 or GIF
if output_path[-3:] == 'mp4':
print(' Exporting animation to {}'.format(output_path))
ani.save(output_path, dpi=width_pixels / 10.0)
elif output_path[-3:] == 'wmv':
print(' Exporting animation to {}'.format(output_path))
ani.save(output_path, dpi=width_pixels / 10.0,
writer=animation.FFMpegFileWriter(fps=1000 / interval, bitrate=4000, codec='wmv2'))
elif output_path[-3:] == 'gif':
print(' Exporting animation to {}'.format(output_path))
ani.save(output_path, dpi=width_pixels / 10.0, writer='imagemagick')
else:
print(' Output file type must be either .mp4, .wmv or .gif')
else:
print('Please select either one or three bands that all exist in the input dataset')
else:
print('At least one x, y or time dimension does not exist in the input dataset. Please use the `time_dim`,' \
'`x_dim` or `y_dim` parameters to override the default dimension names used for plotting')
def animated_doubletimeseries(ds1, ds2, output_path,
width_pixels=1000, interval=200,
bands1=['red', 'green', 'blue'], bands2=['red', 'green', 'blue'],
percentile_stretch1 = (0.02, 0.98), percentile_stretch2 = (0.02, 0.98),
image_proc_func1=None, image_proc_func2=None,
title1=False, title2=False,
show_date1=True, show_date2=True,
annotation_kwargs1={}, annotation_kwargs2={},
onebandplot_cbar1=True, onebandplot_cbar2=True,
onebandplot_kwargs1={}, onebandplot_kwargs2={},
shapefile_path1=None, shapefile_path2=None,
shapefile_kwargs1={}, shapefile_kwargs2={},
time_dim1 = 'time', x_dim1 = 'x', y_dim1 = 'y',
time_dim2 = 'time', x_dim2 = 'x', y_dim2 = 'y'):
"""
Takes two xarray time series and animates both side-by-side as either three-band (e.g. true or false colour)
or single-band animations, allowing changes in the landscape to be compared across time.
Animations can be exported as .mp4 (ideal for Twitter/social media), .wmv (ideal for Powerpoint) and .gif
(ideal for all purposes, but can have large file sizes) format files, and customised to include titles and
date annotations for each panel or use different input bands from each dataset. For example, true and false
colour band combinations could be plotted at the same time, or different products (i.e. NBAR and NBART) or
cloud masking algorithms could be compared.
A shapefile boundary can be added to the output animation by providing a path to the shapefile.
This function can be used to produce visually appealing cloud-free animations when used in combination with
the `load_clearlandsat` function from `dea-notebooks/10_Scripts/DEADataHandling`.
Last modified: October 2018
Author: Robbi Bishop-Taylor, Sean Chua, Bex Dunn
:param ds1:
An xarray dataset with multiple time steps (i.e. multiple observations along the `time_dim` dimension) to be
plotted in the left panel of the animation.
:param ds2:
A matching xarray dataset with the same number of pixels as ds1, to be plotted in the right panel of the
animation. ds1 and ds2 do not need to have exactly the same number of timesteps, but the animation will
only continue up until the length of the shorted dataset (i.e. if `ds1` has 10 timesteps and `ds2` has 5,
the animation will continue for 5 timesteps).
:param output_path:
A string giving the output location and filename of the resulting animation. File extensions of '.mp4',
'.wmv' and '.gif' are accepted.
:param width_pixels:
An optional integer defining the output width in pixels for the resulting animation. The height of the
animation is set automatically based on the dimensions/ratio of `ds1`. Defaults to
1000 pixels wide.
:param interval:
An optional integer defining the milliseconds between each animation frame used to control the speed of
the output animation. Higher values result in a slower animation. Defaults to 200 milliseconds between
each frame.
:param bands1:
An optional list of either one or three bands to be plotted, all of which must exist in `ds1`.
Defaults to `['red', 'green', 'blue']`.
:param bands2:
An optional list of either one or three bands to be plotted, all of which must exist in `ds2`.
Defaults to `['red', 'green', 'blue']`.
:param percentile_stretch1:
An optional tuple of two floats that can be used to clip one or three-band arrays in the left `ds1` panel
by percentiles to produce a more vibrant, visually attractive image that is not affected by outliers/extreme
values. The default is `(0.02, 0.98)` which is equivalent to xarray's `robust=True`.
:param percentile_stretch2:
An optional tuple of two floats that can be used to clip one or three-band arrays in the right `ds2` panel
by percentiles to produce a more vibrant, visually attractive image that is not affected by outliers/extreme
values. The default is `(0.02, 0.98)` which is equivalent to xarray's `robust=True`.
:param image_proc_func1:
An optional function can be passed to modify three-band arrays in the left `ds1` panel for each timestep
prior to animating. This could include image processing functions such as increasing contrast, unsharp
masking, saturation etc. The function should take AND return a three-band numpy array with shape [:, :, 3].
If your function has parameters, you can pass in custom values using `partial` from `functools`:
`image_proc_func=partial(custom_func, param1=10)`.
:param image_proc_func2:
An optional function can be passed to modify three-band arrays in the right `ds1` panel for each timestep
prior to animating. This could include image processing functions such as increasing contrast, unsharp
masking, saturation etc. The function should take AND return a three-band numpy array with shape [:, :, 3].
If your function has parameters, you can pass in custom values using `partial` from `functools`:
`image_proc_func=partial(custom_func, param1=10)`.
:param title1:
An optional string or list of strings with a length equal to the number of timesteps in `ds1`. This can be
used to display a static title for the left panel (using a string), or a dynamic title (using a list)
that displays different text for each timestep. Defaults to False, which plots no title.
:param title2:
An optional string or list of strings with a length equal to the number of timesteps in `ds2`. This can be
used to display a static title for the left panel (using a string), or a dynamic title (using a list)
that displays different text for each timestep. Defaults to False, which plots no title.
:param show_date1:
An optional boolean that defines whether or not to plot date annotations for each animation frame in the
left panel. Defaults to True, which plots date annotations for `ds1`.
:param show_date2:
An optional boolean that defines whether or not to plot date annotations for each animation frame in the
right panel. Defaults to True, which plots date annotations for `ds2`.
:param annotation_kwargs1:
An optional dict of kwargs for controlling the appearance of `ds1` text annotations to pass to
matplotlib `plt.annotate` (see https://matplotlib.org/api/_as_gen/matplotlib.pyplot.annotate.html).
For example, `annotation_kwargs1={'fontsize':20, 'color':'red', 'family':'serif'}. By default, text
annotations are white, size 15 mono-spaced font with a 3pt black outline in the panel's top-right.
:param annotation_kwargs2:
An optional dict of kwargs for controlling the appearance of the `ds2` text annotations to pass
to matplotlib `plt.annotate` (see above).
:param onebandplot_cbar1:
An optional boolean indicating whether to include a colourbar if `ds1` is a one-band array. Defaults to True.
:param onebandplot_cbar2:
An optional boolean indicating whether to include a colourbar if `ds2` is a one-band array. Defaults to True.
:param onebandplot_kwargs1:
An optional dict of kwargs for controlling the appearance of `ds1` one-band image arrays to pass to
matplotlib `plt.imshow` (see https://matplotlib.org/api/_as_gen/matplotlib.pyplot.imshow.html for options).
This only applies if an xarray with a single band is passed to d1. For example, a green colour scheme and
custom stretch can be specified using: `onebandplot_kwargs1={'cmap':'Greens`, 'vmin':0.2, 'vmax':0.9}`.
By default, one-band arrays are plotted using the 'Greys' cmap with bilinear interpolation.
Two special kwargs (`tick_fontsize`, `tick_colour`) can also be passed to control the tick labels on the
colourbar. This can be useful for example when the tick labels are difficult to see against a dark background.
:param onebandplot_kwargs2:
An optional dict of kwargs for controlling the appearance of `ds2` one-band image arrays to
pass to matplotlib `plt.imshow`; only applies if an xarray with a single band is passed to `d2` (see above).
:param shapefile_path1:
An optional string or list of strings giving the file paths of shapefiles to overlay on the left `ds1` panel.
The shapefiles must be in the same projection as the input xarray dataset.
:param shapefile_path2:
An optional string or list of strings giving the file paths of shapefiles to overlay on the right `ds2` panel.
The shapefiles must be in the same projection as the input xarray dataset.
:param shapefile_kwargs1:
An optional dict of kwargs to specify the appearance of the left `ds1` panel shapefile overlay to pass to
`GeoSeries.plot` (see http://geopandas.org/reference.html#geopandas.GeoSeries.plot). For example:
`shapefile_kwargs = {'linewidth':2, 'edgecolor':'black', 'facecolor':"#00000000"}`
:param shapefile_kwargs2:
An optional dict of kwargs to specify the appearance of the right `ds2` panelshapefile overlay. For example:
`shapefile_kwargs = {'linewidth':2, 'edgecolor':'black', 'facecolor':"#00000000"}`
:param time_dim1:
An optional string allowing you to override the xarray dimension used for time in `ds1`.
Defaults to 'time'.