{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## ColorLine" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "x = np.linspace(0, 2 * np.pi, 300)\n", "\n", "lats = 20 * np.cos(x)\n", "lons = 20 * np.sin(x)\n", "colors = np.sin(5 * x)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "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": [ "import folium\n", "from folium import features\n", "\n", "\n", "m = folium.Map([0, 0], zoom_start=3)\n", "\n", "color_line = features.ColorLine(\n", " positions=list(zip(lats, lons)),\n", " colors=colors,\n", " colormap=[\"y\", \"orange\", \"r\"],\n", " weight=10,\n", ")\n", "\n", "color_line.add_to(m)\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Marker, Icon, Popup" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([0, 0], zoom_start=1)\n", "mk = features.Marker([0, 0])\n", "pp = folium.Popup(\"hello\")\n", "ic = features.Icon(color=\"red\")\n", "\n", "mk.add_child(ic)\n", "mk.add_child(pp)\n", "m.add_child(mk)\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Vega popup" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "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": [ "import json\n", "\n", "import vincent\n", "\n", "N = 100\n", "\n", "multi_iter2 = {\n", " \"x\": np.random.uniform(size=(N,)),\n", " \"y\": np.random.uniform(size=(N,)),\n", "}\n", "\n", "scatter = vincent.Scatter(multi_iter2, iter_idx=\"x\", height=100, width=200)\n", "data = json.loads(scatter.to_json())\n", "\n", "m = folium.Map([0, 0], zoom_start=1)\n", "mk = features.Marker([0, 0])\n", "p = folium.Popup(\"Hello\")\n", "v = features.Vega(data, width=\"100%\", height=\"100%\")\n", "\n", "mk.add_child(p)\n", "p.add_child(v)\n", "m.add_child(mk)\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Vega-Lite popup" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/filipe/miniconda3/envs/FOLIUM/lib/python3.9/site-packages/altair/utils/deprecation.py:65: AltairDeprecationWarning: load_dataset is deprecated. Use the vega_datasets package instead.\n", " warnings.warn(message, AltairDeprecationWarning)\n" ] }, { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from altair import Chart, load_dataset\n", "\n", "# load built-in dataset as a pandas DataFrame\n", "cars = load_dataset(\"cars\")\n", "\n", "scatter = (\n", " Chart(cars)\n", " .mark_circle()\n", " .encode(\n", " x=\"Horsepower\",\n", " y=\"Miles_per_Gallon\",\n", " color=\"Origin\",\n", " )\n", ")\n", "\n", "vega = folium.features.VegaLite(\n", " scatter,\n", " width=\"100%\",\n", " height=\"100%\",\n", ")\n", "\n", "m = folium.Map(location=[-27.5717, -48.6256])\n", "\n", "marker = folium.features.Marker([-27.57, -48.62])\n", "\n", "popup = folium.Popup()\n", "\n", "vega.add_to(popup)\n", "popup.add_to(marker)\n", "\n", "marker.add_to(m)\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Vega div and a Map\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "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": [ "import branca\n", "\n", "N = 100\n", "\n", "multi_iter2 = {\n", " \"x\": np.random.uniform(size=(N,)),\n", " \"y\": np.random.uniform(size=(N,)),\n", "}\n", "\n", "scatter = vincent.Scatter(multi_iter2, iter_idx=\"x\", height=250, width=420)\n", "data = json.loads(scatter.to_json())\n", "\n", "f = branca.element.Figure()\n", "\n", "# Create two maps.\n", "m = folium.Map(\n", " location=[0, 0],\n", " tiles=\"stamenwatercolor\",\n", " zoom_start=1,\n", " position=\"absolute\",\n", " left=\"0%\",\n", " width=\"50%\",\n", " height=\"50%\",\n", ")\n", "\n", "m2 = folium.Map(\n", " location=[46, 3],\n", " tiles=\"OpenStreetMap\",\n", " zoom_start=4,\n", " position=\"absolute\",\n", " left=\"50%\",\n", " width=\"50%\",\n", " height=\"50%\",\n", " top=\"50%\",\n", ")\n", "\n", "# Create two Vega.\n", "v = features.Vega(data, position=\"absolute\", left=\"50%\", width=\"50%\", height=\"50%\")\n", "\n", "v2 = features.Vega(\n", " data, position=\"absolute\", left=\"0%\", width=\"50%\", height=\"50%\", top=\"50%\"\n", ")\n", "\n", "f.add_child(m)\n", "f.add_child(m2)\n", "f.add_child(v)\n", "f.add_child(v2)\n", "\n", "f" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Vega-Lite div and a Map" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "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": [ "import pandas as pd\n", "\n", "N = 100\n", "\n", "multi_iter2 = pd.DataFrame(\n", " {\n", " \"x\": np.random.uniform(size=(N,)),\n", " \"y\": np.random.uniform(size=(N,)),\n", " }\n", ")\n", "\n", "scatter = Chart(multi_iter2).mark_circle().encode(x=\"x\", y=\"y\")\n", "scatter.width = 420\n", "scatter.height = 250\n", "data = json.loads(scatter.to_json())\n", "\n", "f = branca.element.Figure()\n", "\n", "# Create two maps.\n", "m = folium.Map(\n", " location=[0, 0],\n", " tiles=\"stamenwatercolor\",\n", " zoom_start=1,\n", " position=\"absolute\",\n", " left=\"0%\",\n", " width=\"50%\",\n", " height=\"50%\",\n", ")\n", "\n", "m2 = folium.Map(\n", " location=[46, 3],\n", " tiles=\"OpenStreetMap\",\n", " zoom_start=4,\n", " position=\"absolute\",\n", " left=\"50%\",\n", " width=\"50%\",\n", " height=\"50%\",\n", " top=\"50%\",\n", ")\n", "\n", "\n", "# Create two Vega.\n", "v = features.VegaLite(data, position=\"absolute\", left=\"50%\", width=\"50%\", height=\"50%\")\n", "\n", "v2 = features.VegaLite(\n", " data, position=\"absolute\", left=\"0%\", width=\"50%\", height=\"50%\", top=\"50%\"\n", ")\n", "\n", "f.add_child(m)\n", "f.add_child(m2)\n", "f.add_child(v)\n", "f.add_child(v2)\n", "\n", "f" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### GeoJson" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "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": [ "N = 1000\n", "\n", "lons = +5 - np.random.normal(size=N)\n", "lats = 48 - np.random.normal(size=N)\n", "\n", "data = {\n", " \"type\": \"FeatureCollection\",\n", " \"features\": [\n", " {\n", " \"type\": \"Feature\",\n", " \"geometry\": {\n", " \"type\": \"MultiPoint\",\n", " \"coordinates\": [[lon, lat] for (lat, lon) in zip(lats, lons)],\n", " },\n", " \"properties\": {\"prop0\": \"value0\"},\n", " },\n", " ],\n", "}\n", "\n", "m = folium.Map([48, 5], zoom_start=6)\n", "m.add_child(features.GeoJson(data))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Div" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "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": [ "N = 100\n", "\n", "multi_iter2 = {\n", " \"x\": np.random.uniform(size=(N,)),\n", " \"y\": np.random.uniform(size=(N,)),\n", "}\n", "\n", "scatter = vincent.Scatter(multi_iter2, iter_idx=\"x\", height=250, width=420)\n", "data = json.loads(scatter.to_json())\n", "\n", "f = branca.element.Figure()\n", "\n", "d1 = f.add_subplot(1, 2, 1)\n", "d2 = f.add_subplot(1, 2, 2)\n", "\n", "d1.add_child(folium.Map([0, 0], tiles=\"stamenwatercolor\", zoom_start=1))\n", "d2.add_child(folium.Map([46, 3], tiles=\"OpenStreetMap\", zoom_start=5))\n", "\n", "f" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### LayerControl" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(tiles=None)\n", "\n", "folium.raster_layers.TileLayer(\"OpenStreetMap\").add_to(m)\n", "folium.raster_layers.TileLayer(\"stamentoner\").add_to(m)\n", "\n", "folium.LayerControl().add_to(m)\n", "\n", "m" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.9.0" } }, "nbformat": 4, "nbformat_minor": 1 }