【发布时间】:2022-01-01 12:47:52
【问题描述】:
我构建了一个 Plotly Dash Web 应用程序来显示传感器数据。我想要一张可以选择车站的地图,因此我可以看到时间序列图。
这是我现在的回调:
@app.callback(
Output('time_series1', 'figure'),
Input('map_sensors', 'selectedData'))
def display_selected_data(selectedData):
if selectedData is None: # Plot whole Dataframe if nothing is selected.
fig = px.line(data_frame=df, x='date.utc', y='value', color='location')
return fig
else:
selectedData['points'][0]['customdata'][0] # This line shows me the name of the location and I want to add this to a list
return
我可以在所选数据中显示位置。现在我的问题是,如何将其添加到列表中?
我的目标是像dff2 = df[df.location.isin(selected_locations)] 这样过滤数据框,以便我只绘制选定的位置。
我现在的完整应用:
from jupyter_dash import JupyterDash
import plotly.graph_objs as go
from dash import Dash, dcc, html, Input, Output, State
import pandas as pd
import plotly.express as px
import json
loc = pd.read_csv('location_sensors.csv')
df = pd.read_csv('measurement.csv')
style = {'width': '50%', 'height': '500px', 'float': 'left'}
# Build small example app.
app = dash.Dash(__name__)
styles = {
'pre': {
'border': 'thin lightgrey solid',
'overflowX': 'scroll'
}
}
fig_map = px.scatter_mapbox(loc, lat="lat", lon="lon", hover_name="location",
hover_data={'location':True, 'lat':False, 'lon':False}, zoom=3, height=600,
color='location', mapbox_style="open-street-map")
fig_map.update_layout(clickmode='event+select')
app.layout = html.Div([
dcc.Graph(id='map_sensors', figure=fig_map , className='six columns'),
html.Div([dcc.Graph(
id='time_series1',
style={'height': 400}
),
])
])
@app.callback(
Output('time_series1', 'figure'),
Input('map_sensors', 'selectedData'))
def display_selected_data(selectedData):
if selectedData is None:
fig = px.line(data_frame=df, x='date.utc', y='value', color='location')
return fig
else:
# Here I want to filter the dataframe to the selected locations.
return
if __name__ == '__main__':
app.run_server()
位置 csv 数据:
lat,lon,location
51.20966,4.43182,BETR801
48.83722,2.3939,FR04014
51.49467,-0.13193,London Westminster
时间序列数据: https://github.com/pandas-dev/pandas/blob/master/doc/data/air_quality_long.csv
【问题讨论】:
-
我认为一个问题是,当你点击地图上的一个位置后,它会变灰,当你再次点击同一个灰显的位置时,输入
selectedData将是None这意味着应用不会知道您重新点击了该点。
标签: python plotly plotly-dash