0
votes

I'm trying to visualize some data with Plotly in Jupyter notebook but the choropleth map is not showing up, as you can see from this image.

The data set I'm working on is from https://csbh-dashboard.mckinsey.com/#/data-insights?chart=SP&geo=County&lob=All&metric1=covid_case_count&metric2=covid_death_count_per_100k_pop&tab=Map

I've tried following a few past submissions from here but I have not made any progress. I would appreciate any help in this direction, Thank you.Here is the code so far

import pandas as pd
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode(connected = True)

df = pd.read_csv('State-level-data_31_07_2021.csv')
df.head()

data = dict(type='choropleth',
        locations = df['State'],
        locationmode = 'USA-states',
        colorscale = 'Greens',
        text = df['State Code'],
        z = df['Cases of COVID-19'],
        colorbar = {'title':"Cases of COVID-19"}
        )
       
layout = dict(title = 'Cases of COVID-19 in USA Map',
          geo = dict(scope='usa')
         )

choromap = go.Figure(data = [data],layout = layout)
iplot(choromap)
1

1 Answers

0
votes

I did not use the data you presented because it requires registration. Instead, I modified your code based on the example in the official reference. I'm not sure what the data format is, but it must be a state code to be specified in the Locations. The code style has been changed to the format used in the official reference. In addition, this is not the cause of the problem.

import pandas as pd
import plotly.graph_objs as go

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv')

df.head()
code state category total exports beef pork poultry dairy fruits fresh fruits proc total fruits veggies fresh veggies proc total veggies corn wheat cotton
0 AL Alabama state 1390.63 34.4 10.6 481 4.06 8 17.1 25.11 5.5 8.9 14.33 34.9 70 317.61
1 AK Alaska state 13.31 0.2 0.1 0 0.19 0 0 0 0.6 1 1.56 0 0 0
2 AZ Arizona state 1463.17 71.3 17.9 0 105.48 19.3 41 60.27 147.5 239.4 386.91 7.3 48.7 423.95
3 AR Arkansas state 3586.02 53.2 29.4 562.9 3.53 2.2 4.7 6.88 4.4 7.1 11.45 69.5 114.5 665.44
4 CA California state 16472.9 228.7 11.1 225.4 929.95 2791.8 5944.6 8736.4 803.2 1303.5 2106.79 34.6 249.3 1064.95
fig = go.Figure(data = go.Choropleth(
    locations = df['code'],
    locationmode = 'USA-states',
    colorscale = 'Greens',
    text = df['code'],
    z = df['total exports'].astype(float),
    colorbar_title = "Total Exports"
))
       
fig.update_layout(
    title_text = 'Total Exports in USA Map',
    geo_scope = 'usa'
)

fig.show()

enter image description here