简体   繁体   English

Plotly 错误“传递给 Graph 的参数无效‘figure.data’”

[英]Plotly error 'Invalid argument `figure.data` passed into Graph'

I'm following a tutorial here to build a plotly dashboard.我正在按照这里的教程来构建一个绘图仪表板。 I've spent the whole day looking for solution or trying to solve it on my own but no luck.我整天都在寻找解决方案或试图自己解决它,但没有运气。 Ideally it should upload a xy, xyy,xyyy... type of data and generate a line plot.理想情况下,它应该上传 xy、xyy、xyyy... 类型的数据并生成线图。

However, after uploading the data I get this error,但是,上传数据后出现此错误, 在此处输入图像描述

This is the sampple data I'm trying to run it on,这是我试图运行它的示例数据,

在此处输入图像描述

Below is my code which is just a copy from above link,下面是我的代码,它只是上面链接的副本,

    import base64
    import datetime
    import io
    import plotly.graph_objs as go
    import cufflinks as cf

    import dash
    from dash.dependencies import Input, Output, State
    import dash_core_components as dcc
    import dash_html_components as html
    import dash_table

    import pandas as pd

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

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

    colors = {
        "graphBackground": "#F5F5F5",
        "background": "#ffffff",
        "text": "#000000"
    }

    app.layout = html.Div([
        dcc.Upload(
            id='upload-data',
            children=html.Div([
                'Drag and Drop or ',
                html.A('Select Files')
            ]),
            style={
                'width': '100%',
                'height': '60px',
                'lineHeight': '60px',
                'borderWidth': '1px',
                'borderStyle': 'dashed',
                'borderRadius': '5px',
                'textAlign': 'center',
                'margin': '10px'
            },
            # Allow multiple files to be uploaded
            multiple=True
        ),
        dcc.Graph(id='Mygraph'),
        html.Div(id='output-data-upload')
    ])

    def parse_data(contents, filename):
        content_type, content_string = contents.split(',')

        decoded = base64.b64decode(content_string)
        try:
            if 'csv' in filename:
                # Assume that the user uploaded a CSV or TXT file
                df = pd.read_csv(
                    io.StringIO(decoded.decode('utf-8')))
            elif 'xls' in filename:
                # Assume that the user uploaded an excel file
                df = pd.read_excel(io.BytesIO(decoded))
            elif 'txt' or 'tsv' in filename:
                # Assume that the user upl, delimiter = r'\s+'oaded an excel file
                df = pd.read_csv(
                    io.StringIO(decoded.decode('utf-8')), delimiter = r'\s+')
        except Exception as e:
            print(e)
            return html.Div([
                'There was an error processing this file.'
            ])

        return df


    @app.callback(Output('Mygraph', 'figure'),
                [
                    Input('upload-data', 'contents'),
                    Input('upload-data', 'filename')
                ])
    def update_graph(contents, filename):
        fig = {
            'layout': go.Layout(
                plot_bgcolor=colors["graphBackground"],
                paper_bgcolor=colors["graphBackground"])
        }

        if contents:
            contents = contents[0]
            filename = filename[0]
            df = parse_data(contents, filename)
            df = df.set_index(df.columns[0])
            fig['data'] = df.iplot(asFigure=True, kind='scatter', mode='lines+markers',                                                 
                size=1)


                    return fig

                @app.callback(Output('output-data-upload', 'children'),
                            [
                                Input('upload-data', 'contents'),
                                Input('upload-data', 'filename')
                            ])
                def update_table(contents, filename):
                    table = html.Div()

                    if contents:
                        contents = contents[0]
                        filename = filename[0]
                        df = parse_data(contents, filename)

                        table = html.Div([
                            html.H5(filename),
                            dash_table.DataTable(
                                data=df.to_dict('rows'),
                                columns=[{'name': i, 'id': i} for i in df.columns]
                            ),
                            html.Hr(),
                            html.Div('Raw Content'),
                            html.Pre(contents[0:200] + '...', style={
                                'whiteSpace': 'pre-wrap',
                                'wordBreak': 'break-all'
                            })
                        ])

                    return table




                if __name__ == '__main__':
                    app.run_server(debug=True)

Since this graph is a cufflinks graph, the graph is displayed by setting it as fig as it is, rather than assigning it as plotly data.由于此图是袖扣图,因此通过将其设置为 fig 来显示该图,而不是将其分配为 plotly 数据。

Instead of this.而不是这个。

fig['data'] = df.iplot(asFigure=True, kind='scatter', mode='lines+markers', size=1)

Correct code正确的代码

fig = df.iplot(asFigure=True, kind='scatter', mode='lines+markers', size=1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 严重错误:“ figure_or_data”参数无效 - Plotly error: Invalid 'figure_or_data' argument 具有绘图错误的等值线图:无效的 figure_or_data 参数 - Choropleth map with plotly error: invalid figure_or_data argument 使用 Dash Python 时出现错误 — 将无效参数“figure”传递到 ID 为“graph”的 Graph。 预期的“对象”。 提供类型“数组” - Getting an error with Dash Python — Invalid argument `figure` passed into Graph with ID “graph”. Expected `object`. Was supplied type `array` 使用 Dash for Figure 作为传递给 Graph 的无效参数 `figure.layout` 出现错误,预期为 `object`。 提供类型`array` - Getting an Error Using Dash for Figure as Invalid argument `figure.layout` passed into Graph, Expected `object`. Was supplied type `array` 绘制带有通过循环附加轨迹的图表时,无效的“ figure_or_data”参数-plotly - Invalid 'figure_or_data' argument when plotting a chart with traces appended through loops - plotly Plotly 错误 - ..my-graph.figure…download.href…的 output 值的数量无效…预期 2,得到 3 - Plotly error - Invalid number of output values for ..my-graph.figure…download.href… Expected 2, got 3 PlotlyError:`figure_or_data` 位置参数必须是 `dict`-like、`list`-like 或 plotly.graph_objs.Figure 的实例 - PlotlyError: The `figure_or_data` positional argument must be `dict`-like, `list`-like, or an instance of plotly.graph_objs.Figure 更新图无效组件道具图时出错 - Error updating graph invalid component prop figure 传递到 ID 为“table”的 DataTable 的参数数据无效 - Invalid argument data passed into DataTable with ID “table” 无效参数错误/图形执行错误 - Invalid Argument Error / Graph Execution Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM