The Complete Guide to Python Map Visualization Libraries: From Folium to GeoPandas

hello, Python Today we're going to learn about the Python Map Visualization Library. Isn't it cool to represent data through maps? I'm sure you've seen a cool interactive map and thought, "Wow, could I make something like that?"

Don't worry, you'll be able to create beautiful map visualizations with this post. We'll go through the different Python map visualization libraries one by one and provide examples that are easy to follow, even for beginners.

Let's dive into the world of map visualization by exploring useful libraries like Folium, GeoPandas, Matplotlib, Plotly, and more, and actually writing some code.

Comparing Python map visualization libraries

To give you a quick comparison of the Python map visualization libraries we're going to introduce, I've put together the table below to help you understand the features and uses of each library, which will help you choose the best one for your project.

LibrariesFeaturesUse cases
FoliumInteractive maps, easy to use, powered by Leaflet.jsCreate interactive maps for web applications
GeoPandasGeospatial data analytics, integration with PandasAnalyze and visualize geospatial datasets
MatplotlibHighly customizable, supports multiple projectionsDetailed, static map visualizations
PlotlySupport for interactive visualizations, 3D and web-based mapsBuild interactive dashboards and 3D maps


This table shows the strengths of each library: Folium makes it easy to create interactive maps for the web, GeoPandas specializes in geographic data analysis, Matplotlib is great for creating static maps that can be fine-tuned, and Plotly has fancy interactive map features and 3D maps.

Choose the right library based on your project goals and the features you need. Sometimes it's a good idea to use a combination of libraries, for example, to analyze data with GeoPandas and visualize it with Folium or Plotly.

1. Folium - Python map visualization library

Folium is one of the most popular libraries for creating interactive maps in Python. It's built on top of Leaflet.js and allows you to display beautiful maps in a web browser.

First, let's install Folium.

pip install folium

Now, let's create a simple map. When you run the code below, it will generate HTML showing a map of Seoul, and if you open the file, you'll see something like this

파이썬 지도 시각화 라이브러리 - Folium 라이브러리 이용
( Map of Seoul, the capital of South Korea )
import folium

# Seoul's latitude and longitude
seoul_lat, seoul_lon = 37.5665, 126.9780

Create a map of #
m = folium.Map(location=[seoul_lat, seoul_lon], zoom_start=12)

Add # marker
folium.Marker([seoul_lat, seoul_lon], popup='Seoul').add_to(m)

Save the # map
m.save('seoul_map.html')

Code commentary:

  1. import folium: Import the Folium library.
  2. seoul_lat, seoul_lon = 37.5665, 126.9780: Store the latitude and longitude of Seoul in a variable.
  3. m = folium.Map(...): Create a map object using Folium. locationis the center coordinates of the map, zoom_startsets the initial zoom level.
  4. folium.Marker(...): Adds a marker at the specified location. popupis the text that appears when the marker is clicked.
  5. m.save('seoul_map.html')Saves the generated map as an HTML file.

This will create an interactive map of the center of Seoul. Isn't that cool?

2. GeoPandas: a powerhouse for geographic data processing

GeoPandas is a library that specializes in working with geographic data. Think of it as an extension of Pandas. Let's take advantage of this Python map visualization library.

Installing GeoPandas can be a little tricky, try installing it with the command below.

pip install geopandas

Now we'll use GeoPandas to draw a simple world map.

GeoPandas로 그린 세계지도 이미지
(World map drawn with GeoPandas)
import geopandas as gpd
import matplotlib.pyplot as plt

Load the # Natural Earth data directly from the URL
url = "https://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_countries.zip"
world = gpd.read_file(url)

Draw a # map
world.plot(figsize=(15, 10))

Add a # title
plt.title('World Map', fontsize=16)

Save the # map
plt.savefig('world_map.png')
plt.show()

Code commentary:

  1. import geopandas as gpd
    • Import the GeoPandas library under the alias 'gpd'.
    • A Python library for processing geospatial data.
  2. import matplotlib.pyplot as plt
    • Import the pyplot module from Matplotlib under the alias 'plt'.
    • The most widely used data visualization library in Python.
  3. url = "https://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_countries.zip"
    • Specify the URL of the world map data provided by Natural Earth.
    • 110m means a resolution of 1:110,000,000 scale.
  4. world = gpd.read_file(url)
    • Reads geodata directly from a URL and stores it as a GeoDataFrame.
    • Automatically process ZIP files and import geodata.
  5. world.plot(figsize=(15, 10))
    • Visualize the imported world map data.
    • Set the size to 15 inches wide and 10 inches tall with figsize=(15, 10).
  6. plt.title('World Map', fontsize=16)
    • Add the title "World map" to the top of the map.
    • Set the font size to 16 points with fontsize=16.
  7. plt.savefig('world_map.png')
    • Save the generated map as a 'world_map.png' file.
    • It is saved in the current working directory in PNG format.
  8. plt.show()
    • Display the generated map on the screen.
    • In interactive environments (such as Jupyter notebooks), it is displayed inline.
  9. In a normal Python script, it would be displayed in a separate window.

3. Matplotlib: Back-to-basics visualization

Matplotlib is Python's default visualization library and can also be used for map visualizations, but it can be a bit complicated because you have to manipulate map data directly.

If necessary pip install basemap command to install additional packages. Let's take advantage of this Python map visualization library.

Matplotlib로 그린 세계 지도 이미지
(World map plotted with Matplotlib)
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

Create a # map
m = Basemap(projection='mill', llcrnrlat=-90, urcrnrlat=90, llcrnrlon=-180, urcrnrlon=180, resolution='c')

Draw # coastlines and borders
m.drawcoastlines()
m.drawcountries()

Draw # latitude, longitude
m.drawparallels(range(-90, 91, 30), labels=[1,0,0,0,0])
m.drawmeridians(range(-180, 181, 60), labels=[0,0,0,0,1])

# Add a title
plt.title('World Map by Matplotlib')

Save and display the # map
plt.savefig('matplotlib_world_map.png')
plt.show()

Code commentary:

  1. from mpl_toolkits.basemap import Basemap: Imports the Basemap module.
  2. m = Basemap(...): Create a map object. Here you set the projection and the extent of the map.
  3. m.drawcoastlines(), m.drawcountries(): Draw a coastline and border.
  4. m.drawparallels(...), m.drawmeridians(...): Draw a latitude and longitude line.
  5. plt.title(...): Adds a title to the map.
  6. plt.savefig(...), plt.show(): Save the map and display it on the screen.

Matplotlib is highly tunable, so you can customize your maps to your liking!

4. Plotly: the king of interactive visualizations

Plotly is a powerful library for creating interactive visualizations, and it's also very useful for map visualizations. In this tutorial, we'll use the Plotly Python map visualization library.

Plotly 그린 서울 지도
( Plotly drawn map of Seoul )
import plotly.graph_objects as go

Create the # map data
fig = go.Figure(go.Scattermapbox(
    mode = "markers",
    lon = [126.9780],
    lat = [37.5665],
    marker = {'size': 20}))

Set the # map layout
fig.update_layout(
    mapbox = {
        'style': "open-street-map",
        'center': { 'lon': 126.9780, 'lat': 37.5665},
        'zoom': 10},
    showlegend = False)

Show the # map
fig.show()

Code commentary:

  1. import plotly.graph_objects as go: Get Plotly's graph object.
  2. fig = go.Figure(go.Scattermapbox(...)): Creates an object that places a marker on the map.
  3. fig.update_layout(...)Sets the style of the map, center coordinates, zoom level, etc.
  4. fig.show(): Displays the generated map on the screen.

Maps created with Plotly are really cool and interactive - you can zoom and pan the map with your mouse!

So there you have it, the different map visualization libraries in Python, each with their own features, strengths, and weaknesses. Folium makes it simple to create interactive maps, GeoPandas is great for geographic data analysis, Matplotlib is highly tunable, and Plotly offers fancy interactivity.

Choose a library that fits your project or data and give it a try. It may seem daunting at first, but keep practicing and before you know it, you'll find yourself creating beautiful map visualizations!

By the way, one of the reasons you look at maps is to travel, right? Python Map Visualization: See a route you'll never regret (feat. folium package) Why not check out our post review to see where you're headed?

# Glossary

  1. Visualization: A graphical representation of data. Map visualization refers to the representation of geographic data in the form of a map.
  2. Library: A collection of code that is a collection of frequently used functions in programming. In Python, the import statement to load and use the library.
  3. InteractiveInteractivity refers to the ability to interact with the user. An interactive map can be zoomed in, out, panned, and more.
  4. Latitude and LongitudeA coordinate system that describes a location on Earth. Latitude is the angle from north to south relative to the equator, and longitude is the angle from east to west relative to the prime meridian.
  5. MarkerA dot or icon that marks a specific location on a map.
  6. Zoom LevelA number that indicates how zoomed in the map is. The higher the number, the more detailed the map.
  7. ProjectionA way to represent the three-dimensional Earth on a two-dimensional plane. There are many different projections, each with their own advantages and disadvantages.
테리 이모티콘
(Happy coding!)

Similar Posts