使用plotly对美国疫情地图可视化

代码如下:


import plotly.graph_objects as go
import pandas as pd
from plotly.offline import plot

codes = ['MA', 'WA', 'CA', 'OR', 'WI', 'ME', 'MI', 'NV', 'NM', 'CO', 'WY', 'KS', 'NE', 'OK', 'MO', 'IL', 'IN', 'VT',
         'AR', 'TX', 'RI', 'AL', 'MS', 'NC', 'VA', 'IA', 'MD', 'DE', 'PA', 'NJ', 'NY', 'ID', 'SD', 'CT', 'NH', 'KY',
         'OH', 'TN', 'WV', 'DC', 'LA', 'FL', 'GA', 'SC', 'MN', 'MT', 'ND', 'AZ', 'UT', 'HI', 'AK']
confirmed = []
ma_num = 130
other_num = 0
for i in codes:
    if i == 'MA':
        confirmed.append(ma_num)
    else:
        confirmed.append(other_num)
df = pd.DataFrame({'codes': codes, 'confirmed': confirmed})

fig = go.Figure(data=go.Choropleth(
    locations=df['codes'],  # 设置位置,各州的编号(缩写)
    z=df['confirmed'].astype(float),  # 设置填充色数据
    locationmode='USA-states',  # 设置国家名称
    hovertext=df['codes'],
    colorscale='Reds',  # 图例颜色
    colorbar_title="Confirmed Number",  # 图例标题
))

fig.update_layout(
    title_text='American Convid-9 Counts',  # 地图标题
    geo_scope='usa',  # 设置地图的范围为美国
    # scope可选有"world","usa","europe","asia","africa","north america","south america"
)
fig.data[0].hovertemplate = 'City: %{hovertext}' + \
                            '
Confirmed Number : %{z}'
plot(fig, filename='MA.html')

可视化结果:
使用plotly对美国疫情地图可视化_第1张图片

你可能感兴趣的:(教程)