InĀ [1]:
import folium
import requests
import pandas
InĀ [3]:
world_map = folium.Map((30, 0), zoom_start=2, tiles="https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryTopo/MapServer/tile/{z}/{y}/{x}",
attr='Tiles courtesy of the <a href="https://usgs.gov/">U.S. Geological Survey</a>')
geojson_data = requests.get(
"https://raw.githubusercontent.com/python-visualization/folium-example-data/main/world_countries.json"
).json()
land_defenders_attacks = pandas.read_csv(
"attacks_by_country_for_folium.csv"
)
land_defenders_map = folium.Choropleth(
name="Map of lethal attacks against land defenders",
geo_data=geojson_data,
data=land_defenders_attacks,
columns=["country", "number_of_victims"],
key_on="feature.properties.name",
fill_color="YlOrRd",
bins=[1, 25, 50, 100, 150, 200, 250, 300, 342], #bins=9,
highlight=True,
nan_fill_color="grey",
fill_opacity=0.8,
line_opacity=0.6,
line_weight=0.5,
legend_name="Number of attacks",
).add_to(world_map)
land_defenders_indexed = land_defenders_attacks.set_index('country')
for feature in land_defenders_map.geojson.data['features']:
try:
feature['properties']['number of victims'] = repr(land_defenders_indexed.loc[feature['properties']['name'], 'number_of_victims'])
except KeyError:
feature['properties']['number of victims'] = "no data"
folium.GeoJsonTooltip(['name', 'number of victims']).add_to(land_defenders_map.geojson)
folium.LayerControl().add_to(world_map)
world_map
Out[3]:
Make this Notebook Trusted to load map: File -> Trust Notebook
InĀ [4]:
world_map.save("land-defenders-map.html")