我正在尝试在dash应用程序上绘制简单的医疗数据,并且此数据具有“医院名称”列。现在我想要的是下拉列表中的医院,因为当有人选择医院时,将绘制其数据。这是应用程序,但失败了
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objects as go
from plotly.offline import iplot
import pandas as pd
import numpy as np
# intialise data of lists.
data = {'Name':['Nick hospital', 'Nick hospital','Nick hospital', 'Krish hospital', 'Krish hospital','Krish hospital'],
'NAR_forms_used':[2, 1,2, 2, 2,3]
}
# Create DataFrame
df = pd.DataFrame(data
hosp_list = df["Name"].unique()
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
dcc.Graph(id='graph'),
dcc.Dropdown(
id="Hosp_list",
options=[{"label": i, "value": i} for i in hosp_list],
multi=True,
value=list(),
)
])
@app.callback(
Output('graph', 'figure'),
[Input('Hosp_list', 'value')])
def update_figure(Hosp_list):
# get counts per NAR type
df_nar=pd.DataFrame(df.groupby('Name')['NAR_forms_used'].value_counts())
df_nar=df_nar.rename({'NAR_forms_used': 'Doc count'}, axis='columns')
df_nar=df_nar.reset_index()
# Manage NAR types (who knows, there may be more types with time?)
nars = df_nar['NAR_forms_used'].unique()
nars = nars.tolist()
nars.sort(reverse=False)
# set up plotly figure
fig = go.Figure()
# add one trace per NAR type and show counts per hospital
for nar in nars:
# subset dataframe by NAR type
df_ply=df_nar[df_nar['NAR_forms_used']==nar]
# add trace
fig.add_trace(go.Bar(x=df_ply['Name'], y=df_ply['NAR count'], name='NAR Type='+str(nar)))
# make the figure a bit more presentable
fig.update_layout(title='NAR per hospital',
yaxis=dict(title='<i>count of NAR types</i>'),
xaxis=dict(title='<i>Hospital</i>',
)
)
fig.show()
if __name__ == '__main__':
app.run_server(debug=True)
请帮助我修复该应用程序,使其能够运行。最终产品是一款根据下拉菜单中医院选择显示图表的应用程序