I’m trying to draw a simple sine wave with a frequency controlled by a slider.
However, the examples I can find on dash sliders are much too complicated - I cannot find out how to make my slider control the graph.
My program below works - the only thing missing is to connect the slider value to the graph. I tried (just guessing) to add the following, but that didn’t work:
def update_graph(value):
return {
‘data’: [go.Scatter(
y=np.sin(2valuenp.pi*xp))]
}
But what is the correct syntax?
Here is my preliminary program
from jupyter_plotly_dash import JupyterDash
import numpy as np
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objs as go
xp=np.linspace(0, 1,num=99,endpoint=True)
freq=2
yp=np.sin(2freqnp.pi*xp)
app = JupyterDash(‘JupyterDashTest’)
app.layout = html.Div([
dcc.Graph(
id=‘Sine wave’,
figure={
‘data’: [
go.Scatter(
x=xp,
y=yp,
text=‘Actual point’,
mode=‘lines+markers’,
opacity=0.8,
marker={
‘size’: 5,
‘line’: {‘width’: 0.5, ‘color’: ‘blue’}
}
)
],
‘layout’: go.Layout(
xaxis={‘type’: ‘linear’, ‘title’: ‘time (s)’},
yaxis={‘title’: ‘y (m)’},
hovermode=‘closest’
)
}
),
html.Label(‘Frequency slider’),
dcc.Slider(id=‘betaslider’,min=1,max=10,value=2,step=0.1,marks={i: i for i in range(10)}),
html.Div(id=‘slider-output-container’)
])
@app.callback(
dash.dependencies.Output(‘slider-output-container’, ‘children’),
[dash.dependencies.Input(‘betaslider’, ‘value’)])
def update_output(value):
freq=value
return ‘Selected frequency is {} Hz’.format(value)
app
Poul Riis
Denmark