<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# Interactive mapping\n",
    "\n",
    "Alongside static plots, `geopandas` can create interactive maps based on the [folium](https://python-visualization.github.io/folium/) library.\n",
    "\n",
    "Creating maps for interactive exploration mirrors the API of [static plots](../reference/api/geopandas.GeoDataFrame.plot.html) in an [explore()](../reference/api/geopandas.GeoDataFrame.explore.html) method of a GeoSeries or GeoDataFrame.\n",
    "\n",
    "Loading some example data:"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "import geopandas\n",
    "\n",
    "nybb = geopandas.read_file(geopandas.datasets.get_path('nybb'))\n",
    "world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))\n",
    "cities = geopandas.read_file(geopandas.datasets.get_path('naturalearth_cities'))"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "The simplest option is to use `GeoDataFrame.explore()`:"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "nybb.explore()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "Interactive plotting offers largely the same customisation as static one plus some features on top of that. Check the code below which plots a customised choropleth map. You can use `\"BoroName\"` column with NY boroughs names as an input of the choropleth, show (only) its name in the tooltip on hover but show all values on click. You can also pass custom background tiles (either a name supported by folium, a name recognized by `xyzservices.providers.query_name()`, XYZ URL or `xyzservices.TileProvider` object), specify colormap (all supported by `matplotlib`) and specify black outline."
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "nybb.explore( \n",
    "     column=\"BoroName\", # make choropleth based on \"BoroName\" column\n",
    "     tooltip=\"BoroName\", # show \"BoroName\" value in tooltip (on hover)\n",
    "     popup=True, # show all values in popup (on click)\n",
    "     tiles=\"CartoDB positron\", # use \"CartoDB positron\" tiles\n",
    "     cmap=\"Set1\", # use \"Set1\" matplotlib colormap\n",
    "     style_kwds=dict(color=\"black\") # use black outline\n",
    "    )"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "The `explore()` method returns a `folium.Map` object, which can also be passed directly (as you do with `ax` in `plot()`). You can then use folium functionality directly on the resulting map. In the example below, you can plot two GeoDataFrames on the same map and add layer control using folium. You can also add additional tiles allowing you to change the background directly in the map."
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "import folium\n",
    "\n",
    "m = world.explore(\n",
    "     column=\"pop_est\",  # make choropleth based on \"BoroName\" column\n",
    "     scheme=\"naturalbreaks\",  # use mapclassify's natural breaks scheme\n",
    "     legend=True, # show legend\n",
    "     k=10, # use 10 bins\n",
    "     legend_kwds=dict(colorbar=False), # do not use colorbar\n",
    "     name=\"countries\" # name of the layer in the map\n",
    ")\n",
    "\n",
    "cities.explore(\n",
    "     m=m, # pass the map object\n",
    "     color=\"red\", # use red color on all points\n",
    "     marker_kwds=dict(radius=10, fill=True), # make marker radius 10px with fill\n",
    "     tooltip=\"name\", # show \"name\" column in the tooltip\n",
    "     tooltip_kwds=dict(labels=False), # do not show column label in the tooltip\n",
    "     name=\"cities\" # name of the layer in the map\n",
    ")\n",
    "\n",
    "folium.TileLayer('Stamen Toner', control=True).add_to(m)  # use folium to add alternative tiles\n",
    "folium.LayerControl().add_to(m)  # use folium to add layer control\n",
    "\n",
    "m  # show map"
   ],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",<commit_msg>DOC: mention CRS requirement in explore docs (#2255)<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c554e753",
   "metadata": {},
   "source": [
    "# Interactive mapping\n",
    "\n",
    "Alongside static plots, `geopandas` can create interactive maps based on the [folium](https://python-visualization.github.io/folium/) library.\n",
    "\n",
    "Creating maps for interactive exploration mirrors the API of [static plots](../reference/api/geopandas.GeoDataFrame.plot.html) in an [explore()](../reference/api/geopandas.GeoDataFrame.explore.html) method of a GeoSeries or GeoDataFrame.\n",
    "\n",
    "Loading some example data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "caf2fbd5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import geopandas\n",
    "\n",
    "nybb = geopandas.read_file(geopandas.datasets.get_path('nybb'))\n",
    "world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))\n",
    "cities = geopandas.read_file(geopandas.datasets.get_path('naturalearth_cities'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56bf1bcf",
   "metadata": {},
   "source": [
    "The simplest option is to use `GeoDataFrame.explore()`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b484ecc",
   "metadata": {},
   "outputs": [],
   "source": [
    "nybb.explore()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a797389",
   "metadata": {},
   "source": [
    "Interactive plotting offers largely the same customisation as static one plus some features on top of that. Check the code below which plots a customised choropleth map. You can use `\"BoroName\"` column with NY boroughs names as an input of the choropleth, show (only) its name in the tooltip on hover but show all values on click. You can also pass custom background tiles (either a name supported by folium, a name recognized by `xyzservices.providers.query_name()`, XYZ URL or `xyzservices.TileProvider` object), specify colormap (all supported by `matplotlib`) and specify black outline."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "798bf532",
   "metadata": {},
   "source": [
    "<div class=\"alert alert-info\">\n",
    "Note\n",
    "\n",
    "Note that the GeoDataFrame needs to have a CRS set if you want to use background tiles.\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94b4ff24",
   "metadata": {},
   "outputs": [],
   "source": [
    "nybb.explore( \n",
    "     column=\"BoroName\", # make choropleth based on \"BoroName\" column\n",
    "     tooltip=\"BoroName\", # show \"BoroName\" value in tooltip (on hover)\n",
    "     popup=True, # show all values in popup (on click)\n",
    "     tiles=\"CartoDB positron\", # use \"CartoDB positron\" tiles\n",
    "     cmap=\"Set1\", # use \"Set1\" matplotlib colormap\n",
    "     style_kwds=dict(color=\"black\") # use black outline\n",
    "    )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a10291e",
   "metadata": {},
   "source": [
    "The `explore()` method returns a `folium.Map` object, which can also be passed directly (as you do with `ax` in `plot()`). You can then use folium functionality directly on the resulting map. In the example below, you can plot two GeoDataFrames on the same map and add layer control using folium. You can also add additional tiles allowing you to change the background directly in the map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cba9970b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import folium\n",
    "\n",
    "m = world.explore(\n",
    "     column=\"pop_est\",  # make choropleth based on \"BoroName\" column\n",
    "     scheme=\"naturalbreaks\",  # use mapclassify's natural breaks scheme\n",
    "     legend=True, # show legend\n",
    "     k=10, # use 10 bins\n",
    "     legend_kwds=dict(colorbar=False), # do not use colorbar\n",
    "     name=\"countries\" # name of the layer in the map\n",
    ")\n",
    "\n",
    "cities.explore(\n",
    "     m=m, # pass the map object\n",
    "     color=\"red\", # use red color on all points\n",
    "     marker_kwds=dict(radius=10, fill=True), # make marker radius 10px with fill\n",
    "     tooltip=\"name\", # show \"name\" column in the tooltip\n",
    "     tooltip_kwds=dict(labels=False), # do not show column label in the tooltip\n",
    "     name=\"cities\" # name of the layer in the map\n",
    ")\n",
    "\n",
    "folium.TileLayer('Stamen Toner', control=True).add_to(m)  # use folium to add alternative tiles\n",
    "folium.LayerControl().add_to(m)  # use folium to add layer control\n",
    "\n",
    "m  # show map"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
{'fraction_non_alphanumeric': 0.16758010118043845, 'fraction_numerical': 0.007693929173693086, 'mean_word_length': 3.0275891341256367, 'pattern_counts': {'":': 98, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1557880', 'n_tokens_mistral': 3347, 'n_tokens_neox': 3212, 'n_words': 901}