Search code examples
pythongeopandasfolium

interactive plot geopandas doesn't show


I have this code from this website: https://geopandas.org/en/stable/docs/user_guide/interactive_mapping.html

import geopandas as gpd
import matplotlib.pyplot as plt

world_filepath = gpd.datasets.get_path('naturalearth_lowres')
world = gpd.read_file(world_filepath)
print(world.head())

world.explore(column='pop_est',cmap='Set2')

plt.show()

I try to run the code, the geodataframe data is printed but no plot is shown. What am i missing?

Thnx in advanced.


Solution

  • world.explore(column='pop_est',cmap='Set2') should return and display a folium map object so you shouldn't need that plt.show() as the bottom.

    Also, since you're using an IDE (not jupyter) we need to write the map to disk then open it up in the broswer.

    Try

    import geopandas as gpd
    import webbrowser
    import os
    
    world_filepath = gpd.datasets.get_path('naturalearth_lowres')
    world = gpd.read_file(world_filepath)
    print(world.head())
    
    # world.explore() returns a folium map
    m = world.explore(column='pop_est',cmap='Set2')
    
    # and then we write the map to disk
    m.save('my_map.html')
    
    # then open it
    webbrowser.open('file://' + os.path.realpath('my_map.html'))
    

    enter image description here