Skip to main content
Chemistry LibreTexts

5.2.8.2: Using ipyleaflet for Interactive Mapping

  • Page ID
    278611
  • 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)]

     

    • Was this article helpful?