Python

Dplotly.graph_objects.choropleth

A choropleth map, or Choropleth for short, is a map made up of colored polygons that describe data.

This tutorial will cover how to create a choropleth map using go.Choropleth.

Plotly.graph_objects.Choropleth

The class syntax is shown below:

class plotly.graph_objects.Choropleth(arg=None, autocolorscale=None, coloraxis=None, colorbar=None, colorscale=None, customdata=None, customdatasrc=None, featureidkey=None, geo=None, geojson=None, hoverinfo=None, hoverinfosrc=None, hoverlabel=None, hovertemplate=None, hovertemplatesrc=None, hovertext=None, hovertextsrc=None, ids=None, idssrc=None, legendgroup=None, legendgrouptitle=None, legendrank=None, locationmode=None, locations=None, locationssrc=None, marker=None, meta=None, metasrc=None, name=None, reversescale=None, selected=None, selectedpoints=None, showlegend=None, showscale=None, stream=None, text=None, textsrc=None, uid=None, uirevision=None, unselected=None, visible=None, z=None, zauto=None, zmax=None, zmid=None, zmin=None, zsrc=None, **kwargs)

 
You can create a Choropleth object by passing the required variables. These important parameters include:

    1. Arg – represents a dictionary of properties that match a choropleth constructor.
    2. Autocolorscale ––specifies the color scale palette, such as default or custom. This is a Boolean value.
    3. Geojson – specifies the GeoJSON data required for the choropleth trace. If no value is determined, plotly will use the default base map.
    4. Locations – sets the coordinate using the location IDs or names.
    5. Locationmode – this parameter specifies the sets of locations used to match the entries in the location’s parameter.
    6. Colorscale – specifies the color scale as an RGB array.
    7. Hoverinfo – specifies the information that appears on the trace on mouse hover.
    8. Text – specifies the text elements that are associated with each location.
    9. Uid – assigns a unique ID of the choropleth trace.
    10. Z – specifies the color values.
    11. Name – specifies the choropleth trace name.
    12. Meta – provides extra metadata information associated with the Choropleth.
    13. Visible – sets if the choropleth object is visible or not.
    14. Ids – assigns a label to each column.

The previous parameters are some of the most common parameters you will need when constructing a choropleth with Plotly’s graph_objects.

Example 1

Let us look at an example of creating a choropleth using the graph_objects. For this example, we will be using Plotly’s sample datasets as provided in the link below:

https://github.com/plotly/datasets

Consider the code shown below:

import plotly.graph_objects as go
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv")
fig = go.Figure(data=go.Choropleth(
    locations=df['code'],
    z = df['total exports'],
    colorscale = 'viridis',
    text = 'Total Exports in USD',
    locationmode = 'USA-states'
))
fig.show()

 
In the previous example, we start by importing graph_objects as go and pandas as pd.

We then load the DataFrame as a CSV from the provided link.

Finally, we create a Choropleth object using the go.Figure() and pass go.Choropleth() as the argument.

We include all the details we want inside the go.Choropleth() to create the target choropleth map.

Running the previous code should create a choropleth map with the total number of exports per state in the USA.

The resulting figure is shown below:

Example 2

If you do not want to view the map of the whole world, you can limit your view scope by setting the scope to ‘usa’.

Consider the example below:

import plotly.graph_objects as go
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv")
fig = go.Figure(data=go.Choropleth(
    locations=df['code'],
    z = df['total exports'],
    colorscale = 'viridis',
    text = 'Total Exports in USD',
    locationmode = 'USA-states',
), layout = dict(geo = dict(scope='usa')))
fig.show()

 
In this example, we pass the layout parameter as a dictionary to the Figure function.

This should limit the resulting figure only to the United States. An example figure is provided below:

Example 3

Plotly also allows us to update a figure using the update_layout() function. Then, we can pass the parameter we wish to update as a dictionary.

Consider an example below:

import plotly.graph_objects as go
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv")
fig = go.Figure(data=go.Choropleth(
    locations=df['code'],
    z = df['total exports'],
    colorscale = 'reds',
    text = 'Total Exports in USD',
    locationmode = 'USA-states',
))
fig.update_layout(
    geo = dict(
        scope = 'usa',
        showlakes=True,
    )
)
fig.show()

 
In this example, we use the update_layout() function to introduce other features, such as showlakes and scope.

The resulting map is shown below:

Conclusion

This article explored how we can create a customized Choropleth map using Plotly’s graph_objects. In addition, important parameters were provided to create the Choropleth object.

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