Skip to content

Instantly share code, notes, and snippets.

@7yl4r
Last active August 18, 2025 21:11
Show Gist options
  • Save 7yl4r/1d4f4e5a50b6fe9d022c562c838f1157 to your computer and use it in GitHub Desktop.
Save 7yl4r/1d4f4e5a50b6fe9d022c562c838f1157 to your computer and use it in GitHub Desktop.
import pandas as pd
import matplotlib.pyplot as plt
from pathlib import Path
# --- CONFIG ---
INDEX = "NDMoI"
AOI = "mangrove_recovery_RB"
# AOI = "mangrove_recovery_JB"
# AOI = "mangrove_death_JB"
# AOI = "mangrove_death_RB"
files = {
"MODIS" : f"{AOI}_mo.csv",
"Landsat-8" : f"{AOI}_l8.csv",
"Sentinel-2": f"{AOI}_s2.csv"
}
date_col = "system:time_start"
# Colorblind-friendly choices (blue & orange)
colors = {
"MODIS" : "#0072B2", # blue
"Landsat-8" : "#E69F00", # orange
"Sentinel-2": "#009E73" # green
}
def makePlot(value_col):
plt.figure(figsize=(12, 6))
for label, fpath in files.items():
f = Path(fpath)
df = pd.read_csv(f, thousands=",")
# Parse and clean
df[date_col] = pd.to_datetime(df[date_col], errors="coerce")
df[value_col] = pd.to_numeric(df[value_col], errors="coerce")
df = df.dropna(subset=[date_col, value_col]).sort_values(date_col)
# Raw points
plt.plot(
df[date_col], df[value_col], "x",
alpha=1, color=colors[label], label=f"{label} raw"
)
# 1-month moving average
monthly = df.set_index(date_col)[value_col].resample("QS").mean() # QS = quarterly
ma = monthly.rolling(window=3).mean()
plt.plot(
ma.index, ma.values, "-",
linewidth=4, color=colors[label], label=f"{label} moving avg"
)
plt.title(f"{value_col} Time Series ({AOI}) with 3-Month Moving Average")
plt.xlabel("Date")
plt.ylabel(value_col)
plt.legend()
plt.grid(True, linestyle="--", alpha=0.6)
plt.tight_layout()
plt.show()
makePlot("NDMoI")
makePlot("NDMaI")
makePlot("NDVI")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment