【问题标题】:Python Plotly ErrorPython 剧情错误
【发布时间】:2018-06-19 17:37:38
【问题描述】:

似乎 plotly 网站上的 choropleth 地图示例代码已过时,不再有效。

我得到的错误是:

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]

我尝试运行的代码如下所示。它是从 plotly 网站按原样复制的。有人对我如何解决它有任何想法吗?

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')

【问题讨论】:

    标签: python plotly mapping


    【解决方案1】:

    fig 应该是 Figure 类型。使用Choropleth 图形对象:

    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)
    ...
    

    【讨论】:

    • 谢谢。我已经更新了代码。似乎已经修复了某些问题,但我仍然收到错误消息:PlotlyDataTypeError:索引“0”处的条目无效,因为它不包含有效的“类型”键值。这是有效的“数据”列表所必需的。错误路径:['data'][0]
    • data = [go.Choropleth( AttributeError: 'module' object has no attribute 'Choropleth'。似乎 Choropleth 不是 go 的选项?感谢您的帮助!
    • 奇怪...这显然是一个选项as shown by the source code。确保 plotly 是最新的pip install plotly --upgrade,然后启动 python 提示并检查版本import plotly plotly.__version__
    • 我正在使用 Conda。 plotly的版本是v1.3.2。
    • 当前版本是v2.2.3,使用conda install -c conda-forge plotly更新并确保您正在导入新模块
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    • 1970-01-01
    • 1970-01-01
    • 2014-09-03
    • 2018-01-15
    • 2020-04-30
    • 1970-01-01
    相关资源
    最近更新 更多