How To Plot Longitude and Latitude Data On A Map In 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.
There are several ways to plot geographical data, you can use
plt.scatter() from matplotlib,
px.scatter_geo() from plotly or even
In this article, we will show exactly how to plot longitude and latitude data using these methods.
Free Bonus: Click Here To Get A FREE Introduction To Working with Geospatial Data in Python and learn how to manipulate and augment real-world data using their geographic dimension.
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 use 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 worldwide.
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. plt.scatter() from matplotlib to plot Geographical Data
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
plt.scatter() from Matplotlib.
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.
import pandas as pd import matplotlib.pyplot as plt df = pd.read_csv('Global-Power-Plant.csv') plt.scatter(x=df['Longitude'], y=df['Latitude']) plt.rcParams["figure.figsize"] = (50,35) plt.show()
Line 1 – 2: Importing our graphical and data packages
Line 4: Reading our CSV file with
Line 6: Plotting our latitude land longitude data using
Line 7-8: Adjusting the figure size and outputting our graph
2. px.scatter_geo() from Plotly to Graph Geographical Data
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
px.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.
px.scatter_geo() function also provides zoom-in features which allow you to zoom into a specified space.
I have also inputted a
hover_name attribute of the powerplant’s name, which provides you the name of the powerplant when you hover over a specific data point within the map.
!pip install plotly import pandas as pd import plotly.express as px df = pd.read_csv('Global-Power-Plant.csv') fig = px.scatter_geo(df, lat='Latitude', lon='Longitude', hover_name="powerplant Name") fig.show()
Line 1: Install the plotly package
Line 3 – 4: Import our packages
Line 6: Read our CSV file
Line 8: Using
px.scatter_geo() we firstly declared our dataset
df and assigned the latitude and longitude values, respectively in the attribute
lon attribute, we also added our powerplant names to the
Line 10: Output our 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.
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.
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.