我是 Dash 的新手,在尝试根据 Range 滑块的范围/值创建可视化过滤器时遇到了一些问题。我在下面附上了我正在使用的代码块,但我似乎无法开始工作。我查看了文档,但不知道这个问题是否与 RangeSliders 或我如何尝试通过“and”命令过滤数据框有关。
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table_experiments as dt
import pandas as pd
import plotly
import plotly.graph_objs as go
#Define Dash App
app=dash.Dash()
df = pd.DataFrame({
'Range1': [1, 2, 3, 4, 5, 6],
'Range2': [6, 7, 8, 9, 10, 11],
})
app.layout = html.Div([
html.Div([
html.H2('Sliders'),
html.H4('Range1'),
dcc.RangeSlider(
id='rangeslider_range1',
min=df['Range1'].min(),
max=df['Range1'].max(),
marks={str(range1): str(range1) for range1 in df['Range1'].unique()},
value = [df['Range1'].min(),df['Range1'].max()]
),
html.H4('Range2'),
dcc.RangeSlider(
id='rangeslider_range2',
min=df['Range2'].min(),
max=df['Range2'].max(),
marks={str(range2): str(range2) for range2 in df['Range2'].unique()},
value = [df['Range2'].min(),df['Range2'].max()]
),
],style={'width': '30%', 'display': 'inline-block'}),
html.Div([
dcc.Graph(id='graph_test'),
],
style={'width': '60%', 'display': 'inline-block', 'float': 'right'})
]
)
@app.callback(
dash.dependencies.Output('graph_test', 'figure'),
[dash.dependencies.Input('rangeslider_range1', 'value'),
dash.dependencies.Input('rangeslider_range2', 'value')#,
])
def update_graph(
rangeslider_range1,
rangeslider_range2
):
filtered_data = df[df['Range1'] == rangeslider_range1 and df['Range2'] == rangeslider_range2]
return {
'data': [go.Scatter(
x=filtered_data['Range1'],
y=filtered_data['Range2'],
mode='markers',
)],
'layout': go.Layout(
xaxis={
'title': 'Range1',
},
yaxis={
'title': 'Range2',
},
hovermode='closest'
)
}
if __name__ == '__main__':
app.run_server()