How to Plot Latitude and Longitude on a Map in Python

In this tutorial, we will learn how to plot geographical coordinates (latitude and longitude) on a map in Python. Mapping data is an integral part of data visualization and analysis, especially when dealing with geospatial data. We will use geospatial libraries in Python to achieve our goal.

Step One: Installation

Before we begin, we need to ensure we have the required libraries: Pandas, Matplotlib, and Geopandas. You can install them using pip:

Step Two: Importing Libraries

Once installed, we import these libraries into our Python script.

Step Three: Creating a Dataframe

We assume that our raw data includes columns for latitude and longitude. Here’s an example dataframe:

{'City': ['New York', 'Los Angeles', 'Chicago'],
 'Latitude': [40.7128, 38.9072, 41.8781],
 'Longitude': [-74.0060, -77.0369, -87.6298]}

Step Four: Transforming the Data

Now, we convert our dataframe into a GeoDataFrame, which is a data structure in Geopandas. This lets us handle geometrical data types (like points, lines, and polygons). We convert the latitudes and longitudes into Points:

Step Five: Plotting the Data

We need a World map on which we can plot our points. Geopandas includes a built-in dataset, which can be loaded as a GeoDataFrame:

Now, we plot our World map and points:

The above code adds our coordinates to the map as red circles.

Step Six: Finishing the Plot

To complete our map, we add a title, and display it:

Complete Code

Here is the full script:

Output

Conclusion

You’ve learned how to plot latitude and longitude coordinates onto a map in Python using pandas, geopandas, and matplotlib. This will allow you to work with and visualize geospatial data effectively in your future projects.