-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot_on_map.py
More file actions
48 lines (38 loc) · 1.53 KB
/
plot_on_map.py
File metadata and controls
48 lines (38 loc) · 1.53 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
import pandas as pd
import folium
from folium import plugins
def plot_on_map(csv_file_path):
# Read the CSV file
data = pd.read_csv(csv_file_path)
# Extract latitude, longitude, and the difference
latitudes = data['latitude']
longitudes = data['longitude']
differences = data['Difference']
# Ensure differences are within the range 0 to 10
differences = differences.clip(0, 10)
coordinates = list(zip(latitudes, longitudes))
# Create a folium map centered around the average latitude and longitude
m = folium.Map(location=[latitudes.mean(), longitudes.mean()], zoom_start=12, tiles='CartoDB positron')
# Create a color map: smaller differences are greener, larger differences are redder
colormap = folium.LinearColormap(colors=['green', 'yellow', 'red'], vmin=0.0, vmax=10.0)
colormap.caption = 'Difference'
# Add circle markers to the map
for coord, diff in zip(coordinates, differences):
color = colormap(diff)
folium.CircleMarker(
location=coord,
radius=7,
color='none',
fill=True,
fill_color=color,
fill_opacity=0.75
).add_to(m)
# Add the colormap to the map
m.add_child(colormap)
# Save the map to an HTML file
output_file_path = csv_file_path.replace('.csv', '_map.html')
m.save(output_file_path)
print(f"Map saved to: {output_file_path}")
# Example usage
csv_file_path = 'results/Hongkong_population density_withoutCOT_3.5Tuning_bias.csv'
plot_on_map(csv_file_path)