In [1]:
%store -r
You will also need to import any libraries you are using in this notebook, since they won’t carry over from the previous notebook:
In [2]:
# Import libraries
import json
import os
import pathlib
import shutil
import matplotlib
from glob import glob
import earthpy.api.appeears as eaapp
import earthpy
import geopandas as gpd
import hvplot.pandas
import hvplot.xarray
import pandas as pd
import rioxarray as rxr
import xarray as xr
import holoviews as hv
STEP 3: Plot NDVI¶
Try It: Plot the change in NDVI spatially
Complete the following:
- Select data from before the ( to )
- Take the temporal mean (over the date, not spatially)
- Get the NDVI variable (should be a DataArray, not a Dataset)
- Repeat for the data from after the ( to )
- Subtract the pre-event data from the post-event data
- Plot the result using a diverging color map like
cmap=plt.cm.PiYG
There are different types of color maps for different types of data. In this case, we want decreases to be a different color from increases, so we should use a diverging color map. Check out available colormaps in the matplotlib documentation.
Looking for an Extra Challenge?
For an extra challenge, add the boundary to the plot.
In [3]:
#calling annual_ndvi_da variable
# Before event plotting
pre_ndvi = (
annual_ndvi_da
.sel(date=slice('2001','2012'))
.mean(dim = 'date')
.NDVI
)
pre_ndvi.plot()
Out[3]:
<matplotlib.collections.QuadMesh at 0x78504bf57150>
In [4]:
# After event plotting
post_ndvi = (
annual_ndvi_da
.sel(date=slice('2013','2025'))
.mean(dim = 'date')
.NDVI
)
post_ndvi.plot()
Out[4]:
<matplotlib.collections.QuadMesh at 0x78504b7e0110>
In [ ]:
# Compute the difference in NDVI before and after
ndvi_diff = post_ndvi - pre_ndvi
# Plot the difference
ndvi_diff_plot = (
ndvi_diff.hvplot(x='x', y='y', cmap='PiYG', geo=True,title = 'NDVI Change Mapping')
*
boundary_gdf.hvplot(geo=True, fill_color=None, line_color='black')
)
hv.save(ndvi_diff_plot,'Tubarjal_NDVI_Difference.html')
ndvi_diff_plot
STEP -1: Wrap up¶
Don’t forget to store your variables so you can use them in other
notebooks! Replace var1
and var2
with the variable you want to save,
separated by spaces.
Finally, be sure to Restart
and Run all
to make sure your notebook
works all the way through!
In [ ]:
%%capture
%%bash
#Coverting to HTML
jupyter nbconvert vegetation-03-plot.ipynb --to html