Choropleth Maps using Plotly in Python - GeeksforGeeks

05 Nov, 2021
Plotly is a Python library that is very popular among data scientists to create interactive data visualizations. One of the visualizations available in Plotly is Choropleth Maps.  Choropleth maps are used to plot maps with shaded or patterned areas which are proportional to a statistical variable. They are composed of colored polygons. They are used for representing spatial variations of a quantity.
To create them, we require two main types of inputs –              
Geometric information –this can be a GeoJSON file (here each feature has an id or some identifying value in properties, orthis can be built-in geometries of plotly – US states and world countries
this can be a GeoJSON file (here each feature has an id or some identifying value in properties, or
this can be built-in geometries of plotly – US states and world countries
A list of values with feature identifier as index
Syntax – plotly.express.choropleth((data_frame=None, lat=None, lon=None, locations=None, locationmode=None, geojson=None, color=None, scope=None, center=None, title=None, width=None, height=None)
Parameters:
lat = this value is used to position marks according to latitude on a map
long = this value is used to position marks according to longitude on a map
locations = this value is interpreted according to locationmode and mapped to longitude/latitude.
locationmode = one of ‘ISO-3’, ‘USA-states’, or ‘country names’. this determines the set of locations used to match entries in locations to regions on the map.
geojson = contains a Polygon feature collection, with IDs, which are references from locations
color = used to assign color to marks
scope = possible values – ‘world’, ‘usa’, ‘europe’, ‘asia’, ‘africa’, ‘north america’, or ‘south america’`Default is `’world’ unless projection is set to ‘albers usa’, which forces ‘usa’
center = sets the center point of the map
Example:
Python3
# code for creating choropleth map of USA states# import plotly libraryimport plotly # import plotly.express module# this module is used to create entire figures at onceimport plotly.express as px # create figurefig = px.choropleth(locationmode="USA-states", color=[1], scope="usa") fig.show()
Output:
A choropleth map can be used to highlight or depict specific areas. The implementation of achieving such functionality is given below.
Example:
Python3
#code for representing states of USA#pass list of states in locations#list will have two-letter abbreviations of statesfig = px.choropleth(locations=["CA","TX","NY"], locationmode="USA-states", color=[1,2,3], scope="usa") fig.show()
Output:
In this example, we will take a dataset of US-states and create a choropleth map for US Agriculture Exports by USA in 2011.
Dataset Link – Click here
Example:
Python3
#import librariesimport pandas as pdimport plotly.express as px #import datadata = pd.read_csv('2011_us_ag_exports.csv') # create choropleth map for the data# color will be the column to be color-coded# locations is the column with sppatial coordinatesfig = px.choropleth(data, locations='code',                    locationmode="USA-states", color='total exports', scope="usa") fig.show()
Output:
rajeev0719singh
Picked
Python-Plotly
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()