-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_visualizations.py
More file actions
84 lines (73 loc) · 2.83 KB
/
generate_visualizations.py
File metadata and controls
84 lines (73 loc) · 2.83 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
84
import os
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Create a directory for visualizations
visualization_dir = 'visualizations'
if not os.path.exists(visualization_dir):
os.makedirs(visualization_dir)
# Load combined data
combined_data = pd.read_csv('combined_trends_data.csv', index_col=0, parse_dates=True)
# Define key global events related to climate change
global_events = {
pd.Timestamp('2009-12-01'): 'COP15 Copenhagen',
pd.Timestamp('2015-12-01'): 'Paris Agreement',
pd.Timestamp('2021-11-01'): 'COP26 Glasgow'
}
# Line Graphs
def plot_line_graphs():
for city in combined_data['City'].unique():
city_data = combined_data[combined_data['City'] == city]
plt.figure(figsize=(12, 6))
for keyword in city_data.columns:
if keyword == 'City':
continue
plt.plot(city_data.index, city_data[keyword], label=keyword)
plt.title(f'Temporal Trends in {city}')
plt.xlabel('Year')
plt.ylabel('Search Interest')
plt.legend()
plt.grid(True)
# Annotate global events
for date, event in global_events.items():
plt.axvline(x=date, color='gray', linestyle='--', linewidth=0.8)
plt.text(date, plt.ylim()[1] * 0.9, event, rotation=45, fontsize=8, color='gray')
plt.tight_layout()
plt.savefig(f'{visualization_dir}/{city.lower().replace(" ", "_")}_line_graph.png')
plt.close()
# Heatmaps
def plot_heatmaps():
for keyword in combined_data.columns:
if keyword == 'City':
continue
plt.figure(figsize=(10, 6))
pivot_table = combined_data.pivot_table(index=combined_data.index, columns='City', values=keyword)
sns.heatmap(pivot_table, cmap='coolwarm', cbar_kws={'label': 'Search Interest'})
plt.title(f'Heatmap for {keyword}')
plt.xlabel('City')
plt.ylabel('Time')
plt.tight_layout()
plt.savefig(f'{visualization_dir}/{keyword}_heatmap.png')
plt.close()
# KDE Plots
def plot_kde_plots():
for keyword in combined_data.columns:
if keyword == 'City':
continue
plt.figure(figsize=(10, 6))
for city in combined_data['City'].unique():
city_data = combined_data[combined_data['City'] == city][keyword]
if city_data.var() > 0: # Check for variance
sns.kdeplot(city_data, label=city)
plt.title(f'KDE Plot for {keyword}')
plt.xlabel('Search Interest')
plt.ylabel('Density')
plt.legend()
plt.tight_layout()
plt.savefig(f'{visualization_dir}/{keyword}_kde.png')
plt.close()
# Generate all visualizations
plot_line_graphs()
plot_heatmaps()
plot_kde_plots()
print("Visualizations complete. Check the 'visualizations' directory for results.")