You've already forked donnees_meteo
Uniformisation des graphiques basiques
This commit is contained in:
106
scripts/plot_basic_variables.py
Normal file
106
scripts/plot_basic_variables.py
Normal file
@@ -0,0 +1,106 @@
|
||||
# scripts/plot_basic_variables.py
|
||||
"""Génère des séries temporelles simples (7 jours) pour chaque variable météo."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
|
||||
from meteo.dataset import load_raw_csv
|
||||
from meteo.plots import export_plot_dataset
|
||||
from meteo.variables import Variable, VARIABLES
|
||||
|
||||
|
||||
CSV_PATH = Path("data/weather_minutely.csv")
|
||||
DEFAULT_OUTPUT_DIR = Path("figures/basic")
|
||||
|
||||
|
||||
def _prepare_slice(df: pd.DataFrame, *, last_days: int) -> pd.DataFrame:
|
||||
"""Extrait la fenêtre temporelle souhaitée et applique une moyenne horaire pour lisser la courbe."""
|
||||
|
||||
end = df.index.max()
|
||||
start = end - pd.Timedelta(days=last_days)
|
||||
df_slice = df.loc[start:end]
|
||||
numeric_slice = df_slice.select_dtypes(include="number")
|
||||
if numeric_slice.empty:
|
||||
raise RuntimeError("Aucune colonne numérique disponible pour les moyennes horaires.")
|
||||
return numeric_slice.resample("1h").mean()
|
||||
|
||||
|
||||
def _plot_variable(df_hourly: pd.DataFrame, var: Variable, output_dir: Path) -> Path | None:
|
||||
"""Trace la série pour une variable et retourne le chemin de l'image générée."""
|
||||
|
||||
if var.column not in df_hourly.columns:
|
||||
print(f"⚠ Colonne absente pour {var.key} ({var.column}).")
|
||||
return None
|
||||
|
||||
series = df_hourly[var.column].dropna()
|
||||
if series.empty:
|
||||
print(f"⚠ Aucun point valide pour {var.key} dans l'intervalle choisi.")
|
||||
return None
|
||||
|
||||
output_dir.mkdir(parents=True, exist_ok=True)
|
||||
output_path = output_dir / f"{var.key}_last_7_days.png"
|
||||
|
||||
export_plot_dataset(series.to_frame(name=var.column), output_path)
|
||||
|
||||
plt.figure()
|
||||
plt.plot(series.index, series)
|
||||
plt.xlabel("Temps (UTC)")
|
||||
unit_text = f" ({var.unit})" if var.unit else ""
|
||||
plt.ylabel(f"{var.label}{unit_text}")
|
||||
plt.title(f"{var.label} - Moyenne horaire sur les 7 derniers jours")
|
||||
plt.grid(True)
|
||||
plt.tight_layout()
|
||||
plt.savefig(output_path, dpi=150)
|
||||
plt.close()
|
||||
print(f"✔ Graphique généré : {output_path}")
|
||||
return output_path
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> None:
|
||||
parser = argparse.ArgumentParser(description="Trace les séries simples pour chaque variable météo.")
|
||||
parser.add_argument(
|
||||
"--only",
|
||||
nargs="*",
|
||||
help="Clés de variables à tracer (par défaut : toutes).",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--days",
|
||||
type=int,
|
||||
default=7,
|
||||
help="Nombre de jours à afficher (par défaut : 7).",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--output-dir",
|
||||
type=Path,
|
||||
default=DEFAULT_OUTPUT_DIR,
|
||||
help="Dossier où stocker les figures.",
|
||||
)
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
if not CSV_PATH.exists():
|
||||
raise FileNotFoundError(f"Dataset introuvable : {CSV_PATH}")
|
||||
|
||||
df = load_raw_csv(CSV_PATH)
|
||||
df_hourly = _prepare_slice(df, last_days=args.days)
|
||||
|
||||
selected: list[Variable]
|
||||
if args.only:
|
||||
keys = set(args.only)
|
||||
selected = [var for var in VARIABLES if var.key in keys]
|
||||
missing = keys - {var.key for var in selected}
|
||||
if missing:
|
||||
raise KeyError(f"Variables inconnues : {sorted(missing)}")
|
||||
else:
|
||||
selected = list(VARIABLES)
|
||||
|
||||
for variable in selected:
|
||||
_plot_variable(df_hourly, variable, args.output_dir)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,75 +0,0 @@
|
||||
# scripts/plot_temperature.py
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from datetime import timedelta
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
|
||||
from meteo.dataset import load_raw_csv
|
||||
from meteo.plots import export_plot_dataset
|
||||
|
||||
|
||||
CSV_PATH = Path("data/weather_minutely.csv")
|
||||
OUTPUT_DIR = Path("figures")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
if not CSV_PATH.exists():
|
||||
print(f"⚠ Fichier introuvable : {CSV_PATH}")
|
||||
print(" Assurez-vous d'avoir généré le dataset minuté.")
|
||||
return
|
||||
|
||||
# Chargement du dataset minuté
|
||||
df = load_raw_csv(CSV_PATH)
|
||||
print(f"Dataset minuté chargé : {CSV_PATH}")
|
||||
print(f" Lignes : {len(df)}")
|
||||
print(f" Colonnes : {list(df.columns)}")
|
||||
print(f" Période : {df.index[0]} → {df.index[-1]}")
|
||||
print()
|
||||
|
||||
# On ne garde que les N derniers jours pour un premier graphique
|
||||
last_n_days = 7
|
||||
end = df.index.max()
|
||||
start = end - pd.Timedelta(days=last_n_days)
|
||||
df_slice = df.loc[start:end]
|
||||
|
||||
if df_slice.empty:
|
||||
print("⚠ Aucun point dans l'intervalle choisi.")
|
||||
return
|
||||
|
||||
# Moyenne horaire pour lisser un peu la courbe (colonnes numériques uniquement)
|
||||
numeric_slice = df_slice.select_dtypes(include="number")
|
||||
if numeric_slice.empty:
|
||||
print("⚠ Aucune colonne numérique disponible pour l'agrégation.")
|
||||
return
|
||||
|
||||
df_hourly = numeric_slice.resample("1h").mean()
|
||||
|
||||
print(f"Intervalle tracé : {df_hourly.index[0]} → {df_hourly.index[-1]}")
|
||||
print(f"Nombre de points (moyenne horaire) : {len(df_hourly)}")
|
||||
|
||||
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
||||
output_path = OUTPUT_DIR / "temperature_last_7_days.png"
|
||||
|
||||
dataset_path = export_plot_dataset(df_hourly, output_path)
|
||||
if dataset_path is not None:
|
||||
print(f"✔ Données exportées : {dataset_path}")
|
||||
|
||||
plt.figure()
|
||||
plt.plot(df_hourly.index, df_hourly["temperature"])
|
||||
plt.xlabel("Temps (UTC)")
|
||||
plt.ylabel("Température (°C)")
|
||||
plt.title("Température - Moyenne horaire sur les 7 derniers jours")
|
||||
plt.grid(True)
|
||||
plt.tight_layout()
|
||||
plt.savefig(output_path, dpi=150)
|
||||
print(f"✔ Figure sauvegardée dans : {output_path.resolve()}")
|
||||
|
||||
# Optionnel : afficher la fenêtre graphique si vous lancez ça depuis un environnement graphique
|
||||
# plt.show()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -8,7 +8,7 @@ from typing import Iterable
|
||||
|
||||
|
||||
PLOT_MODULES: tuple[str, ...] = (
|
||||
"scripts.plot_temperature",
|
||||
"scripts.plot_basic_variables",
|
||||
"scripts.plot_calendar_overview",
|
||||
"scripts.plot_all_pairwise_scatter",
|
||||
"scripts.plot_correlation_heatmap",
|
||||
|
||||
Reference in New Issue
Block a user