{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Legend Control" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## step 1: create an ipyleaflet map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipyleaflet import Map, LegendControl" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mymap = Map(center=(-10, -45), zoom=4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mymap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## step 2: create a legend" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By default, you need to provide at least a dictionary with pair key=> the label to display and value=> the desired color. By default, it is titled 'Legend', but you can pass a title as argument as well." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a_legend = LegendControl(\n", " {\"low\": \"#FAA\", \"medium\": \"#A55\", \"High\": \"#500\"},\n", " title=\"Legend\",\n", " position=\"bottomright\",\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mymap.add(a_legend)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 3: manipulate Legend" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Title" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a_legend.title = \"Risk\" ## set title\n", "a_legend.title # get title" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Legend content" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a_legend.legends = {\"el1\": \"#FAA\", \"el2\": \"#A55\", \"el3\": \"#500\"} # set content\n", "a_legend.legends # get content\n", "\n", "a_legend.add_legend_element(\"el5\", \"#000\") # add a legend element" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a_legend.remove_legend_element(\"el5\") # remove a legend element" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Positioning" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a_legend.positioning = \"topright\" # set positioning : possible values are topleft, topright, bottomleft, bottomright\n", "a_legend.positioning # get current positioning" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.5" } }, "nbformat": 4, "nbformat_minor": 4 }