{ "cells": [ { "cell_type": "markdown", "id": "85e44075", "metadata": {}, "source": [ "Getting started\n", "===============\n", "\n", "Installation\n", "------------\n", "\n", "Folium can be installed using\n", "\n", "```\n", "$ pip install folium\n", "```\n", "\n", "If you are using the Conda package manager, the equivalent is\n", "\n", "```\n", "$ conda install folium -c conda-forge\n", "```\n", "\n", "\n", "### Dependencies\n", "\n", "Folium has the following dependencies, all of which are installed automatically\n", "with the above installation commands:\n", "\n", "- branca\n", "- Jinja2\n", "- Numpy\n", "- Requests\n", "\n", "Additional packages may be necessary for some functionality. It will say so in\n", "the documentation where that's the case.\n", "\n", "\n", "Creating a map\n", "---------------\n", "\n", "Here's a basic example of creating a map:" ] }, { "cell_type": "code", "execution_count": 1, "id": "f9cf0161", "metadata": { "execution": { "iopub.execute_input": "2024-02-29T08:58:59.159882Z", "iopub.status.busy": "2024-02-29T08:58:59.159720Z", "iopub.status.idle": "2024-02-29T08:58:59.558353Z", "shell.execute_reply": "2024-02-29T08:58:59.557869Z" } }, "outputs": [], "source": [ "import folium\n", "\n", "m = folium.Map(location=(45.5236, -122.6750))" ] }, { "cell_type": "markdown", "id": "71a02cbb", "metadata": {}, "source": [ "If you are in a Jupyter Notebook, you can display it by asking for the object representation:" ] }, { "cell_type": "code", "execution_count": 2, "id": "2562f91f", "metadata": { "execution": { "iopub.execute_input": "2024-02-29T08:58:59.560516Z", "iopub.status.busy": "2024-02-29T08:58:59.560251Z", "iopub.status.idle": "2024-02-29T08:58:59.568518Z", "shell.execute_reply": "2024-02-29T08:58:59.568129Z" } }, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m" ] }, { "cell_type": "markdown", "id": "83a83380", "metadata": {}, "source": [ "Or you can save it as an HTML file:" ] }, { "cell_type": "code", "execution_count": 3, "id": "867e2dd9", "metadata": { "execution": { "iopub.execute_input": "2024-02-29T08:58:59.570282Z", "iopub.status.busy": "2024-02-29T08:58:59.569986Z", "iopub.status.idle": "2024-02-29T08:58:59.574675Z", "shell.execute_reply": "2024-02-29T08:58:59.574327Z" } }, "outputs": [], "source": [ "m.save(\"index.html\")" ] }, { "cell_type": "markdown", "id": "38ba69e2", "metadata": {}, "source": [ "Choosing a tileset\n", "------------------\n", "\n", "The default tiles are set to `OpenStreetMap`, but a selection of tilesets are also built in." ] }, { "cell_type": "code", "execution_count": 4, "id": "fd44e7d2", "metadata": { "execution": { "iopub.execute_input": "2024-02-29T08:58:59.576289Z", "iopub.status.busy": "2024-02-29T08:58:59.576129Z", "iopub.status.idle": "2024-02-29T08:58:59.583835Z", "shell.execute_reply": "2024-02-29T08:58:59.583473Z" } }, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "folium.Map((45.5236, -122.6750), tiles=\"cartodb positron\")" ] }, { "cell_type": "markdown", "id": "ba0b2f43", "metadata": {}, "source": [ "You can also pass any tileset as a url template. Choose one from https://leaflet-extras.github.io/leaflet-providers/preview/\n", "and pass the url and attribution. For example:\n", "\n", "```\n", "folium.Map(tiles='https://{s}.tiles.example.com/{z}/{x}/{y}.png', attr='My Data Attribution')\n", "```\n", "\n", "Folium also accepts objects from the [xyzservices package](https://github.com/geopandas/xyzservices).\n", "\n", "\n", "Adding markers\n", "--------------\n", "\n", "There are various marker types, here we start with a simple `Marker`. You can add a popup and\n", "tooltip. You can also pick colors and icons." ] }, { "cell_type": "code", "execution_count": 5, "id": "5785c978", "metadata": { "execution": { "iopub.execute_input": "2024-02-29T08:58:59.585735Z", "iopub.status.busy": "2024-02-29T08:58:59.585437Z", "iopub.status.idle": "2024-02-29T08:58:59.596542Z", "shell.execute_reply": "2024-02-29T08:58:59.596166Z" } }, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([45.35, -121.6972], zoom_start=12)\n", "\n", "folium.Marker(\n", " location=[45.3288, -121.6625],\n", " tooltip=\"Click me!\",\n", " popup=\"Mt. Hood Meadows\",\n", " icon=folium.Icon(icon=\"cloud\"),\n", ").add_to(m)\n", "\n", "folium.Marker(\n", " location=[45.3311, -121.7113],\n", " tooltip=\"Click me!\",\n", " popup=\"Timberline Lodge\",\n", " icon=folium.Icon(color=\"green\"),\n", ").add_to(m)\n", "\n", "m" ] }, { "cell_type": "markdown", "id": "6ebcde77", "metadata": {}, "source": [ "Vectors such as lines\n", "---------------------\n", "\n", "Folium has various vector elements. One example is `PolyLine`, which can show linear elements on a map.\n", "This object can help put emphasis on a trail, a road, or a coastline." ] }, { "cell_type": "code", "execution_count": 6, "id": "7c81ee84", "metadata": { "execution": { "iopub.execute_input": "2024-02-29T08:58:59.598420Z", "iopub.status.busy": "2024-02-29T08:58:59.598109Z", "iopub.status.idle": "2024-02-29T08:58:59.606909Z", "shell.execute_reply": "2024-02-29T08:58:59.606593Z" } }, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(location=[-71.38, -73.9], zoom_start=11)\n", "\n", "trail_coordinates = [\n", " (-71.351871840295871, -73.655963711222626),\n", " (-71.374144382613707, -73.719861619751498),\n", " (-71.391042575973145, -73.784922248007007),\n", " (-71.400964450973134, -73.851042243124397),\n", " (-71.402411391077322, -74.050048183880477),\n", "]\n", "\n", "folium.PolyLine(trail_coordinates, tooltip=\"Coast\").add_to(m)\n", "\n", "m" ] }, { "cell_type": "markdown", "id": "9d47ec96", "metadata": {}, "source": [ "Grouping and controlling\n", "------------------------\n", "\n", "You can group multiple elements such as markers together in a `FeatureGroup`. You can select\n", "which you want to show by adding a `LayerControl` to the map." ] }, { "cell_type": "code", "execution_count": 7, "id": "669671c8", "metadata": { "execution": { "iopub.execute_input": "2024-02-29T08:58:59.608728Z", "iopub.status.busy": "2024-02-29T08:58:59.608392Z", "iopub.status.idle": "2024-02-29T08:58:59.620330Z", "shell.execute_reply": "2024-02-29T08:58:59.619984Z" } }, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map((0, 0), zoom_start=7)\n", "\n", "group_1 = folium.FeatureGroup(\"first group\").add_to(m)\n", "folium.Marker((0, 0), icon=folium.Icon(\"red\")).add_to(group_1)\n", "folium.Marker((1, 0), icon=folium.Icon(\"red\")).add_to(group_1)\n", "\n", "group_2 = folium.FeatureGroup(\"second group\").add_to(m)\n", "folium.Marker((0, 1), icon=folium.Icon(\"green\")).add_to(group_2)\n", "\n", "folium.LayerControl().add_to(m)\n", "\n", "m" ] }, { "cell_type": "markdown", "id": "c1fea87c", "metadata": {}, "source": [ "GeoJSON/TopoJSON overlays\n", "-------------------------\n", "\n", "Folium supports both GeoJSON and TopoJSON data in various formats, such as urls,\n", "file paths and dictionaries." ] }, { "cell_type": "code", "execution_count": 8, "id": "e5023185", "metadata": { "execution": { "iopub.execute_input": "2024-02-29T08:58:59.622333Z", "iopub.status.busy": "2024-02-29T08:58:59.621967Z", "iopub.status.idle": "2024-02-29T08:58:59.903451Z", "shell.execute_reply": "2024-02-29T08:58:59.903009Z" } }, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import requests\n", "\n", "m = folium.Map(tiles=\"cartodbpositron\")\n", "\n", "geojson_data = requests.get(\n", " \"https://raw.githubusercontent.com/python-visualization/folium-example-data/main/world_countries.json\"\n", ").json()\n", "\n", "folium.GeoJson(geojson_data, name=\"hello world\").add_to(m)\n", "\n", "folium.LayerControl().add_to(m)\n", "\n", "m" ] }, { "cell_type": "markdown", "id": "7b035acc", "metadata": {}, "source": [ "Choropleth maps\n", "---------------\n", "\n", "Choropleth can be created by binding the data between Pandas DataFrames/Series and Geo/TopoJSON geometries." ] }, { "cell_type": "code", "execution_count": 9, "id": "105f49a6", "metadata": { "execution": { "iopub.execute_input": "2024-02-29T08:58:59.905949Z", "iopub.status.busy": "2024-02-29T08:58:59.905629Z", "iopub.status.idle": "2024-02-29T08:58:59.965043Z", "shell.execute_reply": "2024-02-29T08:58:59.964596Z" } }, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas\n", "\n", "state_geo = requests.get(\n", " \"https://raw.githubusercontent.com/python-visualization/folium-example-data/main/us_states.json\"\n", ").json()\n", "state_data = pandas.read_csv(\n", " \"https://raw.githubusercontent.com/python-visualization/folium-example-data/main/us_unemployment_oct_2012.csv\"\n", ")\n", "\n", "m = folium.Map(location=[48, -102], zoom_start=3)\n", "\n", "folium.Choropleth(\n", " geo_data=state_geo,\n", " name=\"choropleth\",\n", " data=state_data,\n", " columns=[\"State\", \"Unemployment\"],\n", " key_on=\"feature.id\",\n", " fill_color=\"YlGn\",\n", " fill_opacity=0.7,\n", " line_opacity=0.2,\n", " legend_name=\"Unemployment Rate (%)\",\n", ").add_to(m)\n", "\n", "folium.LayerControl().add_to(m)\n", "\n", "m" ] } ], "metadata": { "jupytext": { "text_representation": { "extension": ".mystnb", "format_name": "myst" } }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 5 }