-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplore_data.py
More file actions
50 lines (42 loc) · 1.67 KB
/
explore_data.py
File metadata and controls
50 lines (42 loc) · 1.67 KB
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
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Load dataset
df = pd.read_csv("data/cleaned_climate_data.csv")
# Remove unnamed columns
df = df.loc[:, ~df.columns.astype(str).str.contains("^Unnamed")]
# Ensure 'Year' column
if "Year" not in df.columns:
df.rename(columns={df.columns[0]: "Year"}, inplace=True)
df["Year"] = pd.to_numeric(df["Year"], errors="coerce")
# Month columns numeric conversion
month_cols = [c for c in df.columns if c not in ["Year", "Annual_Avg"]]
df[month_cols] = df[month_cols].apply(pd.to_numeric, errors='coerce')
# Annual average
df["Annual_Avg"] = df[month_cols].mean(axis=1)
df["Annual_Avg"].fillna(method="ffill", inplace=True)
# 1. Line Plot
plt.figure(figsize=(10,5))
plt.plot(df["Year"], df["Annual_Avg"], color="red", label="Annual Avg")
plt.plot(df["Year"], df["Annual_Avg"].rolling(5).mean(), color="blue", label="5-Year Rolling Avg")
plt.xlabel("Year")
plt.ylabel("Temperature Anomaly (°C)")
plt.legend()
plt.title("Global Temperature Anomalies Over Time")
plt.show()
# 2. Histogram
plt.figure(figsize=(8,5))
plt.hist(df["Annual_Avg"].dropna(), bins=30, color="green", edgecolor="black")
plt.xlabel("Temperature Anomaly (°C)")
plt.ylabel("Frequency")
plt.title("Distribution of Annual Temperature Anomalies")
plt.show()
# 3. Correlation Heatmap
numeric_months = [c for c in month_cols if pd.api.types.is_numeric_dtype(df[c])]
if len(numeric_months) > 1:
plt.figure(figsize=(10,6))
sns.heatmap(df[numeric_months].corr(), annot=True, cmap="coolwarm", center=0)
plt.title("Correlation Heatmap of Monthly Temperature Anomalies")
plt.show()
else:
print("⚠️ Not enough numeric columns for correlation heatmap!")