Python

Plotly.Express.Choropleth()

A choropleth map or choropleth plot is a specific plot that uses a map and colored polygons to represent the spatial variations of data.

In this article, we explore how we can create the choropleth maps using the Plotly express module.

Plotly.express.choropleth()

To create a choropleth map using the express module, we use the choropleth() function. The function takes on a syntax as shown in the following:

plotly.express.choropleth(data_frame=None, lat=None, lon=None, locations=None, locationmode=None, geojson=None, featureidkey=None, color=None, facet_row=None, facet_col=None, facet_col_wrap=0, facet_row_spacing=None, facet_col_spacing=None, hover_name=None, hover_data=None, custom_data=None, animation_frame=None, animation_group=None, category_orders=None, labels=None, color_discrete_sequence=None, color_discrete_map=None, color_continuous_scale=None, range_color=None, color_continuous_midpoint=None, projection=None, scope=None, center=None, fitbounds=None, basemap_visible=None, title=None, template=None, width=None, height=None)

The following are the most common function parameters that you need to know:

  1. data_frame – Specifies the variable holding the column used in the plot. You can pass this value as a DataFrame, a dictionary, or an array like object.
  2. Lat -Specifies the values used to position the marks along the latitude marks on a map. You can pass this value as a column name within the DataFrame or a Pandas Series.
  3. Lon – Similar to lat but the values of this parameter are used to place the marks along the longitude on the map.
  4. locations – Specifies the values interpreted according to the locationmode parameter and mapped to the longitude and latitude.
  5. locationmode – Specifies the set of locations used to match the entries in the locations to the regions of the map.
  6. Geojson – Specifies the collection containing the IDs which are referenced by the locations.
  7. color – Specifies the values used to assign a unique color to the marks.
  8. scope – Specifies the map scope. The default values are set to world. The accepted values are as follows:
    1. ‘world’
    2. ‘usa’
    3. ‘europe’
    4. ‘asia’
    5. ‘africa’
    6. ‘north america’
    7. ‘south america’
  9. center – Sets the center point of the map.
  10. title – Provides a title for the choropleth map.
  11. width/height – Sets the width and height of the figure in pixels.

Basic Choropleth Map

We can create a world map using the choropleth function as shown in the following:

import plotly.express as px
fig = px.choropleth(locationmode='USA-states', scope='world', color=[1])
fig.show()

The previous code creates a world map as shown in the following:

Highlight Specific Areas

We can highlight some specific areas on a map using the locations parameter. For example, to highlight the various states, we can run the following code:

import plotly.express as px
fig = px.choropleth(locations=["TX", "CA", "CO"], locationmode='USA-states', scope='usa', color=[1,2,3])
fig.show()

In this example, we specify the states that we wish to highlight using their state code in the locations parameter.

The resulting figure is as shown in the following:

We can accomplish the same in a world map as shown in the following code:

import plotly.express as px
fig = px.choropleth(locations=['Ireland', 'Egypt', 'Canada'], locationmode='country names', scope='world', color=[1,2,3])
fig.show()

The given code returns a choropleth plot with the world map and the previously highlighted countries.

Output:

We can also create a choropleth based on a DataFrame data. For example, we can use the gapminder data from the Plotly data as shown in the following:

import plotly.express as px
df = px.data.gapminder().query('year==2007')
fig = px.choropleth(df, locations='iso_alpha', color='pop')
fig.show()

The given plot returns the following figure:

This figure depicts the world population.

Conclusion

This article covers the fundamentals of creating and working with choropleth maps in Plotly.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list