plotly错误'无效参数`figue.data`传递到Graph'

发布于 2025-02-08 19:36:30 字数 4930 浏览 1 评论 0原文

我正在关注一个教程在这里构建一个情节仪表板。我整天都在寻找解决方案或尝试自己解决,但没有运气。理想情况下,它应该上传一个XY,XYY,XYYY ...数据类型并生成线路图。

但是,在上传数据后,我得到此错误,

这是我要运行的Sampple

数据src =“ https://i.sstatic.net/k7i4x.png” alt =“在此处输入图像说明”>

以下是我的代码,它只是从上方链接中的副本,

    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)

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.

However, after uploading the data I get this error,
enter image description here

This is the sampple data I'm trying to run it on,

enter image description here

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-残月青衣踏尘吟 2025-02-15 19:36:30

由于此图是一个袖扣图,因此通过将其视为无花果来显示图形,而不是将其分配为绘图数据。

而不是这个。

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

正确的代码

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

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.

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文