How To Plot Longitude and Latitude Data Using Python

Longitude and latitude data are geographical points in a map, this is also known as geospatial data, the latitude and longitude data points allow us to pinpoint an exact location of a place within the world.

By doing so we are able to accurately plot data points to symbolize key locations such as a building, a bridge, or maybe a nearby hospital, the list is limitless.

In this article, we will show you 3 ways that you can plot longitude and latitude data using python programming.

What are Longitude and Latitude data?

In order to represent geographical data on a two-dimensional map, we require two variables to put on our x and y-axis, this can be completed using longitude and latitude data, where the latitude data is on Y-axis and longitude on the X axis, by doing so we can get a unique value within a map, this allows identification of unique locations.

Of course, the earth is not flat, nor even perfectly round, so the reality is more complex.  If we were to graph our longitude and latitude data on a flat scalar graph, there will always be distortions. Therefore, a map projection is needed.

To learn more about map projections you can find out more on matplotlib’s basemap toolkit.

But to keep this tutorial simple, we will be using a 2D graph.

Dataset Used To Plot Our Graph

The dataset we will be using is from Kaggle, this dataset contains longitudinal and latitudinal data of powerplants located across the world.

You can download the file on the Kaggle website here: https://www.kaggle.com/jaytilala/global-power-plant

If that link doesn’t work, you can directly download the CSV file here:

The dataset has two columns, one is our latitude data and the other is our longitude data.

1. Matplotlib: Plot Geographical Data using a scatter plot

Due to the nature of the longitude and longitude data, where the values can be positive or negative, this means we can actually represent the data on a scatter graph.

Therefore if we have enough data points within a specific region, the sheer amounts of data points can provide a “hot spot” in the scatter graph which shows a visualization of our geospatial locations.

To illustrate my point, what I did is imported CSV data which has over 20,000 data points of powerplant locations within the world, then graphed the locations using scatter plot function from Matplotlib.

Geographical map using matplotlib
Python code for scatter plot

As shown in the graph above, the sheer amount of data points provided in my CSV file has created an outline of the world map.

2. Plotly: Plot Geographical Data using a plotly scatter graph

Another way to graph our geospatial data is using a python library called “plotly”. Plotly is a well-known python library because of its ability to provide more graphical tools and functions compared to matplotlib.

To graph our longitude and latitude data we can use plotly’s “scatter_geo” function. This function is basically a scatter graph, but with an in-built geographical map that is overlayed below the scatter graph, which makes it easier for representation.

The “Scatter_geo” function also provides zoom-in features which allow you to zoom into a specified space. I have also inputted a “hover_name” of the powerplants name, what this does is provide you the name of the powerplant when you hover over a specific data point within the map.

Plotly geographical scatter graph

3. Plotly: Plot Geographical Data using a plotly density map

A density map provides a visual representation of the points provided in a given area. The reason why we may want to use a density map is to show the concentration of data which allows us to assess the impact of infrastructure investments on surrounding areas.

Plotly’s density map function allows us to provide a visual representation of geographical data through the use of “heat maps” where the more increased intensity of data points localized within a specific area adjusts the color of the “temperature” scale.

Therefore, the bright-colored region indicates a high-density concentration of data points and vice versa for dark-colored regions.

In our case, it shows the places where powerplants are built the most.

plotly density map
Plotly density map

As mentioned previously, plotly has interactive abilities this means you can zoom in to a location and identify values on a microscale.

The benefit of plotly’s density_mapbox function is it allows representation of countries and continent text data, depending on the scale.

Shown below is a visualization of powerplant data within Europe and its countries.

Conclusion

In this article, you have learned how to graph longitude and latitude data using python libraries. Although there are many packages available to graph geographical data, each graph has its own unique advantage and disadvantages, therefore it is recommended to review the dataset you are using and determine the best graph to use.