Fundamentals 4 min read

Creating a US COVID-19 Choropleth Map with Plotly in Python

This tutorial demonstrates how to install Plotly, load COVID‑19 case data from an Excel file using pandas, and generate an interactive US choropleth map that visualizes cumulative confirmed cases by state, concluding with exporting the map to an HTML file.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Creating a US COVID-19 Choropleth Map with Plotly in Python

As of April, the United States has reported over 120,000 confirmed COVID‑19 cases and more than 2,000 deaths, prompting state‑level disaster declarations and travel advisories.

A reader asked how to visualize the US pandemic data with Python, but the PyEcharts library lacks US state map files, so the author turned to Plotly.

Plotly is a modern Python data‑visualization library that creates interactive, browser‑based charts and maps.

First, install the required Plotly package (version 4.5.4 at the time of writing):

# Install plotly module, direct install gets the latest version
pip install plotly

If the installation is slow, use a domestic mirror:

pip install plotly -i https://pypi.tuna.tsinghua.edu.cn/simple

Import the necessary modules:

# Import required modules
import plotly.graph_objects as go
import pandas as pd

Load the COVID‑19 data stored in an Excel file (sheet "1"):

# Load data
df = pd.read_excel('C:/Python/US/US.xlsx', sheet_name='1')

Create the choropleth map, setting state abbreviations as locations, case numbers as the color scale, and customizing the title and geographic scope:

fig = go.Figure(data=go.Choropleth(
    locations=df['code'],  # state abbreviations
    z=df['conNum'].astype(float),  # values for coloring
    locationmode='USA-states',
    colorscale='Reds',
    colorbar_title="人数"
))
fig.update_layout(
    title_text='美国累计确诊人数',
    geo_scope='usa'  # limits map to the United States
)

Finally, export the interactive map to an HTML file for viewing in a web browser:

# Export map as HTML
fig.write_html("C:/Python/US/US.html")

The resulting map highlights states with higher case counts, such as New York, using a red color gradient to reflect the severity of the outbreak.

- END -

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Data visualizationCOVID-19Choropleth
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.