How to show geopandas interactive map with .explore()
Question:
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?
Answers:
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()
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)
- there a subtle requirement of
explore()
. Column names in GeoDataFrame need to be strings. In your case they numbers zero and one.
- rename these columns:
gdf = gdf.rename(columns={c:str(c) for c in gdf.columns})
import geopandas as gpd
from shapely.geometry import Point
import pandas as pd
# simulate dataframe in question, generates a warning, ignore it
world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
df = world["geometry"].centroid.apply(lambda g: pd.Series(g.coords[0][::-1]))
geometry = [Point(xy) for xy in zip(df[1], df[0])]
gdf = gpd.GeoDataFrame(data=df, geometry=geometry, crs="epsg:4326")
# explore does not like column names that are not strings...
gdf = gdf.rename(columns={c:str(c) for c in gdf.columns})
# print(gdf.head())
# print(gdf.dtypes)
gdf.explore()
In case anyone missed it, the real solution to the problem wasn’t casting it to html, but setting the crs.
The GeoDataFrame needs to have a set crs such as 3857 or 4326 or others so that the tiles get plotted by leaflet when using folium.
Buxert posted a code without crs:
gdf = geopandas.GeoDataFrame(df, geometry=geometry)
and as Matthew pointed out in the comments, he used dummy coordinates but also fixed the crs.
gdf = gpd.GeoDataFrame(df, geometry=geometry, crs=4326)
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?
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()
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)
- there a subtle requirement of
explore()
. Column names in GeoDataFrame need to be strings. In your case they numbers zero and one. - rename these columns:
gdf = gdf.rename(columns={c:str(c) for c in gdf.columns})
import geopandas as gpd
from shapely.geometry import Point
import pandas as pd
# simulate dataframe in question, generates a warning, ignore it
world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
df = world["geometry"].centroid.apply(lambda g: pd.Series(g.coords[0][::-1]))
geometry = [Point(xy) for xy in zip(df[1], df[0])]
gdf = gpd.GeoDataFrame(data=df, geometry=geometry, crs="epsg:4326")
# explore does not like column names that are not strings...
gdf = gdf.rename(columns={c:str(c) for c in gdf.columns})
# print(gdf.head())
# print(gdf.dtypes)
gdf.explore()
In case anyone missed it, the real solution to the problem wasn’t casting it to html, but setting the crs.
The GeoDataFrame needs to have a set crs such as 3857 or 4326 or others so that the tiles get plotted by leaflet when using folium.
Buxert posted a code without crs:
gdf = geopandas.GeoDataFrame(df, geometry=geometry)
and as Matthew pointed out in the comments, he used dummy coordinates but also fixed the crs.
gdf = gpd.GeoDataFrame(df, geometry=geometry, crs=4326)