Scatter plot not rendering in dash plotly

Emm

I am building a dashboard on dash plotly that will have multiple x features in a single scatter plot, with each feature either being presented as a line or a line with markers.

I have built a scatter plot according to the requirements I have specified however, when I run my dashboard locally I do not actually see the scatter plot

This is the code I have written

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)

This is a sample of my data

{'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}

}

sumshyftw

Dash requires an input for a successful callback to occur. If you just want to generate the Plotly scatter plot, you do not need a callback and can just put your code into the app layout. I also converted your dictionary into a Pandas dataframe for creation of the plot.

Updated code below:

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related