A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/choropleth-maps-using-plotly-in-python/ below:

Choropleth Maps using Plotly in Python

Choropleth Maps using Plotly in Python

Last Updated : 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 -              

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:

Example:

Python3
# code for creating choropleth map of USA states
# import plotly library
import plotly

# import plotly.express module
# this module is used to create entire figures at once
import plotly.express as px

# create figure
fig = 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 states
fig = 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 libraries
import pandas as pd
import plotly.express as px

#import data
data = 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 coordinates
fig = px.choropleth(data, locations='code',
                    locationmode="USA-states", color='total exports', scope="usa")

fig.show()

Output:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4