DOC: Reformat plotting_with_folium.ipynb (#2494)

reformat plotting_with_folium.ipynb
This commit is contained in:
Clemens Korner
2022-08-04 14:19:28 +02:00
committed by GitHub
parent 1f7160050b
commit 2a6cfb06fb
+29 -24
View File
@@ -40,7 +40,7 @@
"metadata": {},
"outputs": [],
"source": [
"df1 = pd.read_csv('volcano_data_2010.csv')\n",
"df1 = pd.read_csv(\"volcano_data_2010.csv\")\n",
"\n",
"# Keep only relevant columns\n",
"df = df1.loc[:, (\"Year\", \"Name\", \"Country\", \"Latitude\", \"Longitude\", \"Type\")]\n",
@@ -55,7 +55,9 @@
"source": [
"# Create point geometries\n",
"geometry = geopandas.points_from_xy(df.Longitude, df.Latitude)\n",
"geo_df = geopandas.GeoDataFrame(df[['Year','Name','Country', 'Latitude', 'Longitude', 'Type']], geometry=geometry)\n",
"geo_df = geopandas.GeoDataFrame(\n",
" df[[\"Year\", \"Name\", \"Country\", \"Latitude\", \"Longitude\", \"Type\"]], geometry=geometry\n",
")\n",
"\n",
"geo_df.head()"
]
@@ -66,7 +68,7 @@
"metadata": {},
"outputs": [],
"source": [
"world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))\n",
"world = geopandas.read_file(geopandas.datasets.get_path(\"naturalearth_lowres\"))\n",
"df.Type.unique()"
]
},
@@ -80,10 +82,10 @@
},
"outputs": [],
"source": [
"fig, ax = plt.subplots(figsize=(24,18))\n",
"world.plot(ax=ax, alpha=0.4, color='grey')\n",
"geo_df.plot(column='Type', ax=ax, legend=True)\n",
"plt.title('Volcanoes')"
"fig, ax = plt.subplots(figsize=(24, 18))\n",
"world.plot(ax=ax, alpha=0.4, color=\"grey\")\n",
"geo_df.plot(column=\"Type\", ax=ax, legend=True)\n",
"plt.title(\"Volcanoes\")"
]
},
{
@@ -101,7 +103,7 @@
"outputs": [],
"source": [
"# Stamen Terrain\n",
"map = folium.Map(location = [13.406,80.110], tiles = \"Stamen Terrain\", zoom_start = 9)\n",
"map = folium.Map(location=[13.406, 80.110], tiles=\"Stamen Terrain\", zoom_start=9)\n",
"map"
]
},
@@ -112,7 +114,7 @@
"outputs": [],
"source": [
"# OpenStreetMap\n",
"map = folium.Map(location = [13.406,80.110], tiles='OpenStreetMap' , zoom_start = 9)\n",
"map = folium.Map(location=[13.406, 80.110], tiles=\"OpenStreetMap\", zoom_start=9)\n",
"map"
]
},
@@ -123,7 +125,7 @@
"outputs": [],
"source": [
"# Stamen Toner\n",
"map = folium.Map(location = [13.406,80.110], tiles='Stamen Toner', zoom_start = 9)\n",
"map = folium.Map(location=[13.406, 80.110], tiles=\"Stamen Toner\", zoom_start=9)\n",
"map"
]
},
@@ -141,7 +143,7 @@
"outputs": [],
"source": [
"# Use terrain map layer to see volcano terrain\n",
"map = folium.Map(location = [4,10], tiles = \"Stamen Terrain\", zoom_start = 3)"
"map = folium.Map(location=[4, 10], tiles=\"Stamen Terrain\", zoom_start=3)"
]
},
{
@@ -159,12 +161,12 @@
"outputs": [],
"source": [
"# Create a geometry list from the GeoDataFrame\n",
"geo_df_list = [[point.xy[1][0], point.xy[0][0]] for point in geo_df.geometry ]\n",
"geo_df_list = [[point.xy[1][0], point.xy[0][0]] for point in geo_df.geometry]\n",
"\n",
"# Iterate through list and add a marker for each volcano, color-coded by its type.\n",
"i = 0\n",
"for coordinates in geo_df_list:\n",
" #assign a color marker for the type of volcano, Strato being the most common\n",
" # assign a color marker for the type of volcano, Strato being the most common\n",
" if geo_df.Type[i] == \"Stratovolcano\":\n",
" type_color = \"green\"\n",
" elif geo_df.Type[i] == \"Complex volcano\":\n",
@@ -176,16 +178,19 @@
" else:\n",
" type_color = \"purple\"\n",
"\n",
"\n",
" # Place the markers with the popup labels and data\n",
" map.add_child(folium.Marker(location = coordinates,\n",
" popup =\n",
" \"Year: \" + str(geo_df.Year[i]) + '<br>' +\n",
" \"Name: \" + str(geo_df.Name[i]) + '<br>' +\n",
" \"Country: \" + str(geo_df.Country[i]) + '<br>'\n",
" \"Type: \" + str(geo_df.Type[i]) + '<br>'\n",
" \"Coordinates: \" + str(geo_df_list[i]),\n",
" icon = folium.Icon(color = \"%s\" % type_color)))\n",
" map.add_child(\n",
" folium.Marker(\n",
" location=coordinates,\n",
" popup=\n",
" \"Year: \" + str(geo_df.Year[i]) + \"<br>\"\n",
" + \"Name: \" + str(geo_df.Name[i]) + \"<br>\"\n",
" + \"Country: \" + str(geo_df.Country[i]) + \"<br>\"\n",
" + \"Type: \" + str(geo_df.Type[i]) + \"<br>\"\n",
" + \"Coordinates: \" + str(geo_df_list[i]),\n",
" icon=folium.Icon(color=\"%s\" % type_color),\n",
" )\n",
" )\n",
" i = i + 1"
]
},
@@ -218,9 +223,9 @@
"\n",
"from folium import plugins\n",
"\n",
"map = folium.Map(location = [15,30], tiles='Cartodb dark_matter', zoom_start = 2)\n",
"map = folium.Map(location=[15, 30], tiles=\"Cartodb dark_matter\", zoom_start=2)\n",
"\n",
"heat_data = [[point.xy[1][0], point.xy[0][0]] for point in geo_df.geometry ]\n",
"heat_data = [[point.xy[1][0], point.xy[0][0]] for point in geo_df.geometry]\n",
"\n",
"heat_data\n",
"plugins.HeatMap(heat_data).add_to(map)\n",