Mapping Geographical Data in Python
Master Programming with Our Comprehensive Courses Enroll Now!
Using python plotlib is the simplest way to Mapping Geographical Data in Python. The maps are easy to make, interactive, web-friendly, and easily shareable.
Visualising data helps us a lot in data science, which is done through geopandas.
There are plenty of open-source libraries in python to make geographic maps. You can customise them and change their colours and shapes to represent different areas in the maps.
What is Geographical Plotting?
We use geographical plotting to display your complex data on a large global scale and states within a country in a colourful manner. It plays a vital role in data visualisation and data analysis. It is mostly used in building dashboards to print large amounts of data.
Python’s plotly library is the most common way to create colourful maps. Moreover, it helps in making interesting and interactive maps.
Geographical Plotting Using Python Plotly
There are many libraries in python to handle geographical and graph data. One such library in python is the plotly library. The purpose of this library is to help us to draw geographical graphs. It is a free and open-source library that includes various graphs such as line charts, horizontal bar charts, bar charts, scatter plots, pie charts, and so on.
We can use geographical plotting to work with the world map as well as the states of a country. Data analysts use geographical plotting to track agricultural imports and visualize any other forms of data.
Plotly is a python library used to construct graphs that are easy to understand and interactive. To create graphs and charts, we connect plotly with pandas using cufflink. For example, Choropleth defines the geographical plot of the USA.
Plotting Choropleth Maps Using Python
A choropleth map is a special kind that uses colour coding to indicate the various geographical divisions within a map. Let us look at how to draw these choropleth maps in python using the plotly library.
Terminologies
Plotly is an IT computing company that develops data analytics and data visualisation tools. It provides graphical libraries in python for online graphing, analytics, and statistical tools for individuals and businesses. Now, let us look at some terminologies involved in it.
1. Plotly.py
As mentioned before, plotly.py is an interactive, free, open-source library in python to draw and visualise graphs.
To install the library, type the following command in your Jupyter notebooks.
Pip install plotly
Plotly express is a high-level python visualisation library. It is a wrapper for plotly.py, showing the complex charts’ complicated syntaxes that are also a part of plotly.
2. GeoJSON
It is an open standard format representing simple geographical features and their non-spatial attributes. GeoJSON files contain a list of features and non-spatial characteristics that you can use to define geographical features.
3. Mapbox
It is an open-source mapping platform for creating custom design maps.
Project
The first thing we need is to find a GeoJSON file for Indian states. Then Given below is the source code of the project along with the output.
pip install plotly
import json
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.io as pio
import plotly.io as pio
pio.renderers
pio.renderers.default = "browser"
india_states=json.load(open("Downloads/states_india.geojson","r"))
state_id_map = {}
for feature in india_states["features"]:
feature["id"] = feature["properties"]["state_code"]
state_id_map[feature["properties"]["st_nm"]] = feature["id"]
# Libraries needed for the tutorial
import pandas as pd
import requests
import io
# Downloading the csv file from your GitHub account
url = "https://raw.githubusercontent.com/nikhilkumarsingh/choropleth-python-tutorial/master/india_census.csv" # Make sure the url is the raw version of the file on GitHub
download = requests.get(url).content
# Reading the downloaded content and turning it into a pandas dataframe
df = pd.read_csv(io.StringIO(download.decode('utf-8')))
# Printing out the first 5 rows of the dataframe
print (df.head())
df["Density"] = df["Density[a]"].apply(lambda x: int(x.split("/")[0].replace(",", "")))
df["id"] = df["State or union territory"].apply(lambda x: state_id_map[x])
df['DensityScale']=np.log10(df['Density'])
fig= px.choropleth(df,
locations= 'id',
geojson=india_states,
color='DensityScale',
hover_name='State or union territory',
hover_data=['Density']
)
fig.update_geos(fitbounds='locations', visible=False )
fig.show()
Density Maps
Density maps are an easy way to show the concentration of points or lines in a given area.
Using Density maps with Python
In order to make the density maps, we will use the worldwide data set of earthquakes and magnitudes.
We will begin by importing libraries.
import plotly.express as px import pandas as pd
Then we create our dataframe.
The dataset is available online and can be imported using the following commands:
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv')
df.info()
We will use the following lines of code to plot our data:
df.head()
fig = px.density_mapbox(df, lat='Latitude', lon='Longitude', z='Magnitude', radius=10,
center=dict(lat=0, lon=180), zoom=0,
mapbox_style="stamen-terrain")
fig.show()
Conclusion
This was all about Mapping Geographical Data in Python using Plotly library.
