Search code examples
pythongeopandasfolium

How to show geopandas interactive map with .explore()


I made a geopandas dataframe and I want to use geopandas_dataframe.explore() to create an interactive map. Here is my code. First I create the geopandas dataframe, I check the dtypes and I try to map the dataframe with gdf.explore(). Unfortunately, my code just finishes without errors and no map is shown.

code:

geometry = [Point(xy) for xy in zip(df[1], df[0])]
gdf = geopandas.GeoDataFrame(df, geometry=geometry)
print(gdf.head())
print(gdf.dtypes)
gdf.explore()

output:

           0         1                  geometry
0  51.858306  5.778404  POINT (5.77840 51.85831)
1  51.858322  5.778410  POINT (5.77841 51.85832)
2  51.858338  5.778416  POINT (5.77842 51.85834)
3  51.858354  5.778422  POINT (5.77842 51.85835)
4  51.858370  5.778429  POINT (5.77843 51.85837)
0            float64
1            float64
geometry    geometry
dtype: object

Process finished with exit code 0

Why don't I get a map? I already tried gdf.show() but that doesn't exist. What do I need to do to show the geopandas map?


Solution

  • What IDE are you using? In Jupyter Notebook your code (slightly modified) works for me. However, when I run it in PyCharm I get, "Process finished with exit code 0" with no plot.

    import geopandas as gpd
    import pandas as pd
    from shapely.geometry import Point
    
    data_dict = {'x': {0: -110.1, 1: -110.2, 2: -110.3, 3: -110.4, 4: -110.5},
     'y': {0: 40.1, 1: 40.2, 2: 40.3, 3: 40.4, 4: 40.5}}
    
    df = pd.DataFrame(data_dict)
    
    geometry = [Point(xy) for xy in zip(df['x'], df['y'])]
    gdf = gpd.GeoDataFrame(df, geometry=geometry, crs=4326)
    print(gdf.head())
    print(gdf.dtypes)
    gdf.explore()
    

    enter image description here

    Edit: Looks like you can save your folium figure to a html. This worked for me from PyCharm.

    m = gdf.explore()
    
    outfp = r"<your dir path>\base_map.html"
    
    m.save(outfp)