Python | Geographical plotting using plotly

6 min readJul 9, 2021


Geographical plotting is used for world map as well as states under a country. Mainly used by data analysts to check the agriculture exports or to visualize such data.

plotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library.

cufflink connects plotly with pandas to create graphs and charts of dataframes directly. choropleth is used to describe geographical plotting of USA. choropleth is used in the plotting of world maps and many more.

Command to install plotly:

pip install plotly

Below is the implementation:

# Python program to plot

# geographical data using plotly

# importing all necessary libraries

import plotly.plotly as py

import plotly.graph_objs as go

import pandas as pd

# some more libraries to plot graph

from plotly.offline import download_plotlyjs, init_notebook_mode, iplot, plot

# To establish connection

init_notebook_mode(connected = True)

# type defined is choropleth to

# plot geographical plots

data = dict(type = 'choropleth',

# location: Arizoana, California, Newyork

locations = ['AZ', 'CA', 'NY'],

# States of USA

locationmode = 'USA-states',

# colorscale can be added as per requirement

colorscale = 'Portland',

# text can be given anything you like

text = ['text 1', 'text 2', 'text 3'],

z = [1.0, 2.0, 3.0],

colorbar = {'title': 'Colorbar Title Goes Here'})

layout = dict(geo ={'scope': 'usa'})

# passing data dictionary as a list

choromap = go.Figure(data = [data], layout = layout)

# plotting graph




Plotly is a famous library used for creating interactive plotting and dashboards in Python. Plotly is also a company, that allows us to host both online and offline data visualisatoins.

In this article, we will be using offline plotly to visually represent data in the form of different geographical maps.

Installing Plotly

pip install plotly
pip install cufflinks

Run both commands in the command prompt to install plotly and cufflinks and all of their packages on our local machine.

Choropleth Map

Choropleth maps are popular thematic maps used to represent statistical data through various shading patterns or symbols on predetermined geographic areas (i.e., countries). They are good at utilizing data to easily represent the variability of the desired measurement across a region.

How does a Choropleth map works?

Choropleth Maps display divided geographical areas or regions that are colored, shaded, or patterned in relation to a data variable. This provides a way to visualize values over a geographical area, which can show variation or patterns across the displayed location.

Using Choropleth with Python

Here, we will be using a dataset of power consumption of different countries throughout the world in 2014.

Okay, so let’s get started.

Importing libraries

import plotly.graph_objs as go 
from plotly.offline import init_notebook_mode,iplot,plot
import pandas as pd

Here, init_notebook_mode(connected=True) connects Javascript to our notebook.

Creating/Interpreting our DataFrame

df = pd.read_csv('2014_World_Power_Consumption')

Here we have 3 columns, and all of them have 219 non-null entries.


Compiling our data into dictionaries

data = dict(
type = 'choropleth',
colorscale = 'Viridis',
locations = df['Country'],
locationmode = "country names",
z = df['Power Consumption KWH'],
text = df['Country'],
colorbar = {'title' : 'Power Consumption KWH'},

type = ’choropleth’: defines the type of the map, i.e., choropleth in this case.

colorscale = ‘Viridis’: displays a color map (for more color scales, refer here).

locations = df[‘Country’]: add a list of all countries.

locationmode = ‘country names’: as we have country names in our dataset, so we set location mode to ‘country names’.

z: list of integer values that display the power consumption of each state.

text = df[‘Country’]: displays a text when hovering over the map for each state element. In this case, it is the name of the country itself.

colorbar = {‘title’ : ‘Power Consumption KWH’}: a dictionary that contains information about the right sidebar. Here, colorbar contains the title of the sidebar.

layout = dict(title = '2014 Power Consumption KWH',
geo = dict(projection = {'type':'mercator'})

layout — a Geo object that can be used to control the appearance of the base map onto which data is plotted.

It is a nested dictionary that contains all the relevant information about how our map/plot should look like.

Generating our plot/map

choromap = go.Figure(data = [data],layout = layout)

Cool! Our choropleth map for the ‘2014 World Power Consumption’ has been generated, and from the above, we can see that each country displays its name and its power consumption in kWh when hovering over each element on the map. The more concentrated the data is in one particular area, the deeper the shade of color on the map. Here ‘China’ has the largest power consumption, and so its color is deepest.

Density Maps

Density mapping is simply a way to show where points or lines may be concentrated in a given area.

Using Density Maps with Python

Here, we will be using a worldwide dataset of earthquakes and their magnitudes.

Okay, so let’s get started.

Importing libraries

import as px
import pandas as pd

Creating/Interpreting our DataFrame

df = pd.read_csv('')

Here, we have 4 columns, and all of them have 23412 non-null entries.


Plotting our data

fig = px.density_mapbox(df, lat='Latitude', lon='Longitude', z='Magnitude', radius=10,
center=dict(lat=0, lon=180), zoom=0,

lat=’Latitude’: takes in the Latitude column of our data frame.

lon=’Longitude’: takes in the Longitude column of our data frame.

z: list of integer values that display the magnitude of the earthquake.

radius=10: sets the radius of influence of each point.

center=dict(lat=0, lon=180): sets the center point of the map in a dictionary.

zoom=0: sets map zoom level.

mapbox_style=’stamen-terrain’: sets the base map style. Here, “stamen-terrain” is the base map style. displays the map.


Great! Our Density map for the ‘Earthquakes and their magnitudes’ has been generated, and from the above, we can see that it covers all the territories that suffered from the earthquake, and also shows the magnitude of the earthquake of every region when we hover over it.

Geographical plotting using plotly can be a bit challenging sometimes due to its various formats of data, so please refer to this cheat sheet for all types of syntaxes of plotly plots.