✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚾️ It's finally Baseball season! Root for the home team... & Register for our Sports Analytics Webinar!

Choropleth map USA scope

Hi all.

I am using the code graciously supplied here by a user of these forums to construct a Choropleth map in Dash. I am attempting to set the scope of the map to be the United States which is listed as an option here, but it seems to be the only one of those options not presently working. I get an undefined error when I attempt to set the scope to ‘usa’. How can I make the map show the states of the US?

Hum, I investigated a bit and there could be a version conflict between dash and dash-core-components. Could you please try to create a fresh environment and install only the latest release of dash (plus the other dependencies you need?)? Dash now automatically installs dash-html-components and dash-core-components when it’s installed, with compatible versions. For example the code below works for me in a fresh environment (whereas it does not display anything, with a JS error, in an environment where I know that I have these version conflicts)

import os

import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objs as go


df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminder_with_codes.csv')
dff = df.groupby(['iso_alpha', 'country']).mean().reset_index()

 
fig = go.Figure(go.Choropleth(
    locations=dff['iso_alpha'],
    z=dff['lifeExp'],
    text=dff['country'],
    #locationmode='USA-states',
                          ))
 
fig.update_layout(
    geo = dict(
        scope='usa',
        projection=go.layout.geo.Projection(type = 'albers usa'),
        showlakes=True, # lakes
        lakecolor='rgb(255, 255, 255)'),
)       


external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)


app.layout = html.Div([
    dcc.Graph(id="my-graph", figure=fig)
                       ], className="container")
                       
                       
                       
if __name__ == '__main__':
    app.run_server(debug=True)

Hi, looks like my dash-core-components was outdated so updating fixed the issue. Thanks!