1

It seems that the example code on the plotly website for choropleth maps is out of date and no longer works.

The error I'm getting is:

PlotlyError: Invalid 'figure_or_data' argument. Plotly will not be able to properly parse the resulting JSON. If you want to send this 'figure_or_data' to Plotly anyway (not recommended), you can set 'validate=False' as a plot option.
Here's why you're seeing this error:

The entry at index, '0', is invalid because it does not contain a valid 'type' key-value. This is required for valid 'Data' lists.

Path To Error:
['data'][0]

The code that I'm trying to run is shown below. It is copied as-is from the plotly website. Anyone have any ideas as to how I can fix it?

import plotly.plotly as py
import pandas as pd

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

for col in df.columns:
    df[col] = df[col].astype(str)

scl = [[0.0, 'rgb(242,240,247)'],[0.2, 'rgb(218,218,235)'],[0.4, 'rgb(188,189,220)'],\
            [0.6, 'rgb(158,154,200)'],[0.8, 'rgb(117,107,177)'],[1.0, 'rgb(84,39,143)']]

df['text'] = df['state'] + '<br>' +\
    'Beef '+df['beef']+' Dairy '+df['dairy']+'<br>'+\
    'Fruits '+df['total fruits']+' Veggies ' + df['total veggies']+'<br>'+\
    'Wheat '+df['wheat']+' Corn '+df['corn']

data = [ dict(
        type='choropleth',
        colorscale = scl,
        autocolorscale = False,
        locations = df['code'],
        z = df['total exports'].astype(float),
        locationmode = 'USA-states',
        text = df['text'],
        marker = dict(
            line = dict (
                color = 'rgb(255,255,255)',
                width = 2
            )
        ),
        colorbar = dict(
            title = "Millions USD"
        )
    ) ]

layout = dict(
        title = '2011 US Agriculture Exports by State<br>(Hover for breakdown)',
        geo = dict(
            scope='usa',
            projection=dict( type='albers usa' ),
            showlakes = True,
            lakecolor = 'rgb(255, 255, 255)',
        ),
    )

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

url = py.plot(fig, filename='d3-cloropleth-map')

1 Answer 1

1

fig should be of the Figure type. Use the Choropleth graph object:

import plotly.graph_objs as go
...
data = [go.Choropleth(        
        colorscale = scl,
        autocolorscale = False,
        locations = df['code'],
        z = df['total exports'].astype(float),
        locationmode = 'USA-states',
        text = df['text'],
        marker = dict(
            line = dict(
                color = 'rgb(255,255,255)',
                width = 2)),
        colorbar = dict(
            title = "Millions USD")
        )]

...
fig = go.Figure(data=data, layout=layout)
...
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. I've updated the code. Seems to have fixed something, but I'm still getting an error: PlotlyDataTypeError: The entry at index, '0', is invalid because it does not contain a valid 'type' key-value. This is required for valid 'Data' lists. Path To Error: ['data'][0]
data = [go.Choropleth( AttributeError: 'module' object has no attribute 'Choropleth'. Seems that Choropleth isn't an option for go? Thanks for your help on this!
Strange... It's clearly an option as shown by the source code. Make sure plotly is up to date pip install plotly --upgrade and then get a python prompt up and check the version import plotly plotly.__version__
I'm using Conda. The version of plotly is v1.3.2.
Current version is v2.2.3, update using conda install -c conda-forge plotly and make sure you are importing the new module

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.