HzM74
HzM74

Reputation: 109

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.

Upvotes: 1

Views: 2643

Answers (2)

Rob Raymond
Rob Raymond

Reputation: 31146

  • export() creates a folium object, not a matplotlib figure
  • in jupyter just have m as last item in code cell
  • in other environments you can save as HTML and launch into a browser
import geopandas as gpd
import webbrowser
from pathlib import Path

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

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

f = Path.cwd().joinpath("map.html")
m.save(str(f))
webbrowser.open("file://" + str(f))


Upvotes: 0

Bob Haffner
Bob Haffner

Reputation: 8483

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

Upvotes: 2

Related Questions