简体   繁体   中英

Plotly-dash graph not showing values

I am trying to create some chart using Dash for Python. I have some file with values that I want to read in, save the values in a list and use it to create the graph. My code:

    app = dash.Dash()
app.layout = html.Div([
    html.H1('Title'),
    dcc.Dropdown(
        id='my-dropdown',
        options=[
            {'label': 'Fruit', 'value': 'FRUIT'}
           # {'label': 'Tesla', 'value': 'TSLA'},
           # {'label': 'Apple', 'value': 'AAPL'}
        ],
        value='TEMPERATUR'
    ),
    dcc.Slider(
    min=-5,
    max=10,
    step=0.5,
    value=-3,
    ),
    dcc.Graph(id='my-graph', animate=True),
])
path = "/../example.csv"

with open(path,"r") as file:
    reader = csv.reader(file)
    dataCopy=[]
    for line in file: 
        dataCopy.append(line)
    arrayValues = np.array(dataCopy)
@app.callback(Output('my-graph', 'figure'), [Input('my-dropdown', 'value')])

def update_graph(selected_dropdown_value):

    return {
        'data': arrayValues }

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

When I print the arrayValues I get:

['28.687', '29.687', '24.687', '21.687', '25.687', '28.687']

But when I check my graph it has no values shown on it. Do you know what could my mistake be?

UPDATE: I tried with the line

arrayValues = list(map(float, arrayValues))

after getting it as a suggestion in the comments, but still no workable code.

You need to provide some additional information to the Graph data field,

If you want the arrayValues to be plotted in Y axis in a line graph the following code should work.

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import plotly.graph_objs as go

#hardcoding arrayValues since csv is not provided
arrayValues = ['28.687', '29.687', '24.687', '21.687', '25.687', '28.687']

app = dash.Dash()
app.layout = html.Div([
    html.H1('Title'),
    dcc.Dropdown(
        id='my-dropdown',
        options=[
            {'label': 'Fruit', 'value': 'FRUIT'}
           # {'label': 'Tesla', 'value': 'TSLA'},
           # {'label': 'Apple', 'value': 'AAPL'}
        ],
        value='TEMPERATUR'
    ),
    dcc.Slider(
    min=-5,
    max=10,
    step=0.5,
    value=-3,
    ),
    dcc.Graph(id='my-graph', animate=True),
])
#path = "/../example.csv"

#with open(path,"r") as file:
#    reader = csv.reader(file)
#    dataCopy=[]
#    for line in file: 
#        dataCopy.append(line)
#    arrayValues = np.array(dataCopy)



@app.callback(Output('my-graph', 'figure'), [Input('my-dropdown', 'value')])
def update_graph(selected_dropdown_value):

    return {
        'data': [
                {'y': arrayValues}
            ]
    }

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

As Dash uses plotly graph representation, you can refer to the official plotly docs to a variety of such Graphs.

Here is the documentation, https://plot.ly/python/basic-charts/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM