Shan Yi Tan
Shan Yi Tan

Reputation: 21

United States Plotly Choropleth Map - Value Error

I tried to run this following code on JupyterLab, but I got value error. I got the code from: https://plot.ly/python/choropleth-maps/ but I used offline plotly instead. Anyone can help? Thank you!

import plotly.offline as py
py.init_notebook_mode(connected=True)
import plotly.graph_objs as go 
import plotly.tools as tls 
import warnings
from collections import Counter 

    # Load data frame and tidy it.
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv')

fig = go.Figure(data=go.Choropleth(
    locations=df['code'], # Spatial coordinates
    z = df['total exports'].astype(float), # Data to be color-coded
    locationmode = 'USA-states', # set of locations match entries in `locations`
    colorscale = 'Reds',
    colorbar_title = "Millions USD",
))

fig.update_layout(
    title_text = '2011 US Agriculture Exports by State',
    geo_scope='usa', # limite map scope to USA
)

fig.show()

The value error

Upvotes: 0

Views: 1309

Answers (1)

Shan Yi Tan
Shan Yi Tan

Reputation: 21

I changed my code to this and it worked:

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

data = [dict(type = 'choropleth',
            colorscale = 'Reds',
            locations=df['code'], # Spatial coordinates
            z = df['total exports'].astype(float), # Data to be color-coded
            locationmode = 'USA-states', # set of locations match entries in `locations
            colorbar = {'title':"Millions USD"},
           )]

layout = dict(title = '2011 US Agriculture Exports by State',
              geo = dict(scope='usa', showlakes = True)) # limite map scope to USA)

fig = dict( data=data, layout=layout )

url = iplot( fig, filename='d2-cloropleth-map' )

Upvotes: 1

Related Questions