-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdated_Visualization.py
More file actions
83 lines (63 loc) · 2.6 KB
/
Updated_Visualization.py
File metadata and controls
83 lines (63 loc) · 2.6 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
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
import pandas as pd
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import seaborn as sns
from io import BytesIO
import base64
import os
base_dir = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(base_dir, "Final_Dataset.csv")
df = pd.read_csv(file_path)
def plot_to_base64():
fig = plt.gcf()
buf = BytesIO()
fig.savefig(buf, format='png', bbox_inches='tight')
plt.close(fig)
buf.seek(0)
return base64.b64encode(buf.read()).decode('utf-8')
def plot_aqi_histogram(month):
df_filtered = df[df["month"] == month]
aqi_trend = df_filtered.groupby("year")["AQI"].mean()
plt.figure(figsize=(10, 7))
plt.bar(aqi_trend.index, aqi_trend.values, color='b', edgecolor="black")
plt.xlabel("Year")
plt.ylabel("Average AQI")
plt.title(f"Average AQI for Month {month} Across All Years")
plt.xticks(aqi_trend.index, rotation=45)
return plot_to_base64()
def plot_pollutant_contribution(month):
df_filtered = df[df["month"] == month]
pollutant_columns = [col for col in df.columns if col not in ["year", "month", "AQI"]]
df_filtered.loc[:, pollutant_columns] = df_filtered[pollutant_columns].apply(pd.to_numeric, errors='coerce')
pollutant_sums = df_filtered[pollutant_columns].sum()
pollutant_sums = pollutant_sums[pollutant_sums > 0]
if pollutant_sums.empty:
return None
plt.figure(figsize=(8, 8))
plt.pie(pollutant_sums, labels=pollutant_sums.index, autopct="%1.1f%%",
startangle=140, colors=plt.cm.Paired.colors)
plt.title(f"Pollutant Contribution to AQI for Month {month} Across All Years")
return plot_to_base64()
def plot_aqi_trend(month):
df_filtered = df[df["month"] == month]
aqi_trend = df_filtered.groupby("year")["AQI"].mean()
plt.figure(figsize=(10, 5))
plt.plot(aqi_trend.index, aqi_trend.values, marker="o", linestyle="-", color="b",
label=f"Average AQI for Month {month}")
plt.xlabel("Year")
plt.ylabel(f"Average AQI in Month {month}")
plt.title(f"AQI Trend for Month {month} Across All Years")
plt.xticks(aqi_trend.index)
plt.grid(True, linestyle="--", alpha=0.6)
plt.legend()
return plot_to_base64()
def plot_aqi_heatmap(month):
df_filtered = df[df["month"] == month]
heatmap_data = df_filtered.pivot_table(values="AQI", index="year", aggfunc="mean")
plt.figure(figsize=(8, 6))
sns.heatmap(heatmap_data, cmap="coolwarm", annot=True, fmt=".1f", linewidths=0.5)
plt.title(f"AQI Heatmap for Month {month} Across All Years")
plt.xlabel("Year")
plt.ylabel("")
return plot_to_base64()