散点图未在破折号中绘制

Scatter plot not rendering in dash plotly

我正在 dash plotly 上构建一个仪表板,它将在单个散点图中具有多个 x 特征,每个特征要么显示为一条线,要么显示为带有标记的线。

我已经根据我指定的要求构建了一个散点图,但是,当我 运行 我在本地的仪表板时,我实际上并没有看到散点图

这是我写的代码

import dash
import dash_table
import plotly.graph_objs as go
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input,Output
import pandas as pd
import os
import numpy as np
app = dash.Dash()
app.layout = html.Div(children=[
    dcc.Graph(
        id='supervisor'
    )
    ])
@app.callback(dash.dependencies.Output('supervisor','figure'))
def scattertable():
    trace0 = go.Scatter(
        x=supervisor['Características (D)'],
        y=supervisor['Mean Team Performance'],
        mode='lines',
        name='Caracteristicas (D)'
    )
    trace1 = go.Scatter(
        x=supervisor['Características (I)'],
        y=supervisor['Mean Team Performance'],
        mode='lines+markers',
        name='Características (I)'
    )
    trace2 = go.Scatter(
        x=supervisor['Características (S)'],
        y=supervisor['Mean Team Performance'],
        mode='lines',
        name='Características (S)'
    )
    trace3 = go.Scatter(
        x=supervisor['Características (C)'],
        y=supervisor['Mean Team Performance'],
        mode='lines+markers',
        name='Características (C)'
    )
    data = [trace0,trace1,trace2,trace3]
    return {"data": data,
            "layout": go.Layout(title="Relationship",
                                yaxis={"title":'Mean', "range":[0, max(supervisor['Mean Team Performance'])+1]},
                                xaxis={"title":'Characteristics', "tickangle":45}, )}

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

这是我的数据样本

{'Características (D)': {2373: nan, 2361: 67.0, 2349: 65.0},
 'Características (I)': {2373: nan, 2361: 20.0, 2349: 55.0},
 'Características (S)': {2373: nan, 2361: 48.0, 2349: 30.0},
 'Características (C)': {2373: nan, 2361: 90.0, 2349: 85.0},
 'Motivación (D)': {2373: nan, 2361: 69.0, 2349: 59.0},
 'Motivación (I)': {2373: nan, 2361: 25.0, 2349: 58.0},
 'Motivación (S)': {2373: nan, 2361: 65.0, 2349: 30.0},
 'Motivación (C)': {2373: nan, 2361: 84.0, 2349: 93.0},
 'Bajo Stress (D)': {2373: nan, 2361: 69.0, 2349: 69.0},
 'Bajo Stress (I)': {2373: nan, 2361: 30.0, 2349: 60.0},
 'Bajo Stress (S)': {2373: nan, 2361: 40.0, 2349: 40.0},
 'Bajo Stress (C)': {2373: nan, 2361: 92.0, 2349: 74.0},
 'Cost to Company': {2373: 1908.33, 2361: 1908.33, 2349: 1908.33},
 'MonthsofEmploymentRounded': {2373: 1.0, 2361: 4.0, 2349: 4.0},
 'Compensation': {2373: 1200.0, 2361: 1200.0, 2349: 1200.0},
 'span': {2373: 37.0, 2361: 58.0, 2349: 86.0},
 'Mean Team Performance': {2373: 0.40544395205206984,
  2361: 0.5936947689016717,
  2349: 0.5403025332663768},
 'Mean Team Employment in Months': {2373: 8.675675675675675,
  2361: 5.396551724137931,
  2349: 6.174418604651163},
 'employment span': {2373: 43, 2361: 128, 2349: 128}

}

Dash 需要输入才能成功回调。如果您只想生成 Plotly 散点图,则不需要回调,只需将代码放入应用程序布局即可。我还将您的字典转换为 Pandas 数据框以创建情节。

更新了以下代码:

import dash
import dash_table
import plotly.graph_objs as go
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input,Output
import pandas as pd
import os
import numpy as np

supervisor_df = pd.DataFrame.from_dict(supervisor)
fig = go.Figure()
category_dict = {'Características (D)':'lines',
                 'Características (I)':'lines+markers',
                 'Características (S)':'lines',
                 'Características (C)':'lines+markers'}
for category in category_dict.keys():
    fig.add_trace(go.Scatter(
        x=supervisor_df[category],
        y=supervisor_df['Mean Team Performance'],
        mode=category_dict[category],
        name=category
    ))
fig.update_layout(title="Relationship",
            yaxis={"title":'Mean', "range":[0, max(supervisor_df['Mean Team Performance'])+1]},
            xaxis={"title":'Characteristics', "tickangle":45}, )
app = dash.Dash()
app.layout = html.Div(children=[
    dcc.Graph(
        id='supervisor',
        figure=fig.to_dict()
    )
])

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