Skip to main content
Chemistry LibreTexts

5.2.8.2: Using ipyleaflet for Interactive Mapping

  • Page ID
    278611
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The ipyleaflet Jupyter package for Python allows you to use the powerful interactive mapping features of Leafletjs within Jupyter and Libretexts documents. Here's a short example to demonstrate what can be done with ipyleaflet on Libretexts.

    First we import the packages we'll need to use.

    from ipyleaflet import Map, Heatmap
    from random import uniform
    import time
     

    Then, we'll declare and display a map of the world.

    m = Map(center=[0, 0], zoom=2)
    m

    Next, we can make a function to generate random heat maps and overlay them onto our existing map.

    def create_random_data(length):
        "Return a list of some random lat/lon/value triples."
        return [[uniform(-80, 80), 
                 uniform(-180, 180), 
                 uniform(0, 1000)] for i in range(length)]
    
    heat = Heatmap(locations=create_random_data(1000), radius=20, blur=10)
    m.add_layer(heat)
    

    Then, we can even animate it as follows:

    for i in range(100):
        heat.locations = create_random_data(1000)
        time.sleep(0.1)
    
    heat.radius = 30
    heat.blur = 50
    heat.max = 0.5
    heat.gradient = {0.4: 'red', 0.6: 'yellow', 0.7: 'lime', 0.8: 'cyan', 1.0: 'blue'}
    heat.locations = [[uniform(-80, 80), uniform(-180, 180), uniform(0, 1000)] for i in range(1000)]

     


    5.2.8.2: Using ipyleaflet for Interactive Mapping is shared under a CC BY 1.3 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?