【发布时间】:2021-10-28 10:25:02
【问题描述】:
我正在尝试使用一个下拉选择器创建散点图,并希望选择器能够选择多个胶囊和所有胶囊的默认设置。
我知道必须首先将 multi 设置为 True,并且必须调整回调以更新每个后续更改或添加胶囊的图表。我很难找到一种合适的方法来更改代码,以便我可以选择多个胶囊甚至整个胶囊数据集。我还创建了一个唯一胶囊 ID 的列表,并将其用作下拉列表,这样就可以了。
数据看起来像这样。
Dash 看起来像这样。
我在 pycharm 上运行的代码。
import pandas as pd
import plotly.express as px # (version 4.7.0)
import plotly.graph_objects as go
import dash # (version 1.12.0) pip install dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
app = dash.Dash(__name__)
df = pd.read_csv("tcd vs rh.csv")
print(df)
capsuleID = df['Capsule_ID'].unique()
print(capsuleID)
capsuleID_names = list(capsuleID)
print(capsuleID_names)
app.layout = html.Div([
html.H1("Web Application Dashboards with Dash", style={'text-align': 'center'}),
dcc.Dropdown(id="capsule_select",
options=[{"label": capsuleID_names, "value": capsuleID_names} for capsuleID_names in capsuleID_names],
multi=True,
value=2100015,
style={'width': "40%"}
),
html.Div([
dcc.Graph(id="the_graph")
]),
])
# -----------------------------------------------------------
@app.callback(
Output('the_graph', 'figure'),
[Input('capsule_select', 'value')]
)
def update_graph(capsule_chosen):
dff = df[(df['Capsule_ID'] == capsule_chosen)] # filter all rows where capsule ID is the capsule ID selected
scatterplot = px.scatter(
data_frame=dff,
x="tcd",
y="humidity",
)
scatterplot.update_traces(textposition='top center')
return scatterplot
# ------------------------------------------------------------------------------
if __name__ == '__main__':
app.run_server(debug=True)
【问题讨论】:
标签: plotly plotly-dash dashboard plotly-python