As of April, the cumulative number of COVID-19 cases in the United States has exceeded 120,000, with more than 2,000 deaths. The US government has declared a “state of disaster” for 17 states and territories in response to COVID-19. The U.S. Centers for Disease Control and Prevention also issued travel advisories for New York and other hard-hit states on Monday.

A friend asked if you could tell us how to use Python to map the EPIDEMIC situation in the United States.

However, PyEcharts did not have a map file for each state of the United States to map the epidemic in the United States, so it had to find other tools for mapping. Plotly was discovered.

Plotly is a new generation of Python data visualization development library that displays information by building interactive diagrams in browser-based web form. Plotly can create dozens of beautiful charts and maps.

A partial rendering of Plotly is shown below

This is what Plotly’s map looks like, because the cumulative number of confirmed cases in New York is much higher than in other areas.

First let’s install the required modules:

PIP install plotly 4.5.4 PIP install plotlyCopy the code

If the installation is slow or unsuccessful, you can use the following code to change the domestic source address to install

pip install plotly -i 
https://pypi.tuna.tsinghua.edu.cn/simpleCopy the code

Then import the modules you want to use:

Import plotly. Graph_objects as go import pandas as pdCopy the code

Then import the required data, which is stored in Excel:

Import data with the following code:

Df = pd.read_excel('C:/Python/US/ us.xlsx ',sheet_name='1')Copy the code

Now you can create the map and set the parameters as follows:

Choropleth(locations=df['code']) Astype (float), # set fill color data locationMode = 'USA-States ', # set country name colorScale = 'Reds', Title_text =' total number of confirmed cases in US ', geo_scope=' USA ', #scope optional include "world", "USA", "Europe", "Asia", "Africa", "North America", "South America")Copy the code

Finally, export the map, the code is as follows:

FIG. Write_html ("C:/Python/US/ us.html ")Copy the code

Wenyuan network, only for the use of learning, such as infringement, contact deletion.

I have compiled good technical articles and lessons learned on my public account, Python Circle.

You will definitely encounter difficulties in learning Python. Don’t panic, I have a set of learning materials, including 40+ e-books, 600+ teaching videos, covering Python basics, crawlers, frameworks, data analysis, machine learning, etc.