Quantcast
Channel: 🎛️ Dash - Plotly Community Forum
Viewing all articles
Browse latest Browse all 6271

DASH live-update Scatter plot annotation problem

$
0
0

Hello,
I’m having some trouble with annotation using the live-update feature of Dash. I take text from a CSV file and plot two subplots consisting of a box-plot and a scatter-plot. The live-update works great with the plot as the CSV file is updated with new data. However, I wish to annotate the points on the scatter plot. When I initially load the CSV file, all the data and annotations are working correctly but when the graph updates, I lose my box plot. I’d like to think it was a bug in Dash, but I’m convinced I’m just blind to the real reason. Thanks for your help. Please let me know if you have any questions about the code.

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

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

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

trace = []
trace2 = []
hr = []
blood_pressure = []
datex_time = []
scatter_time = []
text_hr = []

app.layout = html.Div([
    html.H2(" BST program"),
    html.Div(
    [
        dcc.Interval(id="interval"),
        html.P(id="output"),
        html.Button("toggle interval", id="button"),
    ]
),

    html.Div(
    [
        dcc.Graph(id='live-graph', animate=False),
        dcc.Interval(
            id='graph-update',
            interval=5000,
            n_intervals=0,),
    ]),
    ])


@app.callback(Output('live-graph', 'figure'),
              [Input('graph-update', 'n_intervals')])
def update_graph_scatter(n):
    with open('record Saturday,May:02.csv', 'r') as rf:
        reader = csv.reader(rf)
        for i in reader:
            blood_pressure.append(eval(i[1]))
            datex_time.append(eval(i[0]))
            hr.append([eval(i[2])])

    for a in datex_time:
        scatter_time.append(a[0])

    for a in hr:
        text_hr.append(a[0])

    for i in range(0, len(blood_pressure)):
        trace.append(go.Box(y=blood_pressure[i],
                            name=scatter_time[i],
                            line=dict(color='#1E90FF'),
                            hoverinfo="all"))
    for a in range(0, len(hr)):
        trace2.append(go.Scatter(y=hr[a],
                                 x=datex_time[a],
                                 name='HR',
                                 marker=dict(size=[15],
                                             color='#B8860B'), text=['(Heart Rate)']))

    fig = make_subplots(rows=1, cols=1)

    def append_trace(trace, trace2):
        for i in range(0, len(trace)):
            fig.append_trace(trace[i], 1, 1)
        for a in range(0, len(trace2)):
            fig.append_trace(trace2[a], 1, 1)
            fig.add_annotation(
                x=scatter_time[a],
                y=text_hr[a],
                xref="x",
                yref="y",
                text="99%<br>40",
                showarrow=True,
                font=dict(
                    family="Courier New, monospace",
                    size=16,
                    color="#ffffff"
                ),
                align="center",
                arrowhead=1,
                arrowsize=0.3,
                arrowwidth=0.3,
                arrowcolor="#636363",
                ax=0,
                ay=-180,
                bordercolor="#c7c7c7",
                borderwidth=2,
                borderpad=4,
                bgcolor="#ff7f0e",
                opacity=0.8
            )
    append_trace(trace, trace2)

    fig.update_yaxes(title_text="Blood Pressure", row=1, col=1)
    fig.update_xaxes(title_text="Time", row=1, col=1)

    fig.layout.update(showlegend=False)

    return fig


@app.callback(
    Output("graph-update", "disabled"),
    [Input("button", "n_clicks")],
    [State("graph-update", "disabled")],
)
def toggle_interval(n, disabled):
    if n:
        return not disabled
    return disabled


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



In these two examples: the data plots loaded from the CSV on start of the program, and the last plot (with missing box) occurs on live-updating even though the plots and functions are defined within the callback.

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 6271

Trending Articles