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):

<code># Install plotly module, direct install gets the latest version
pip install plotly</code>

If the installation is slow, use a domestic mirror:

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

Import the necessary modules:

<code># Import required modules
import plotly.graph_objects as go
import pandas as pd</code>

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

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

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

<code>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
)
</code>

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

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

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 -

PythonCOVID-19PlotlyChoropleth
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

login 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.