Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Dash应用程序没有';t显示基于下拉选择的绘图_Python_Pandas_Plotly_Plotly Dash - Fatal编程技术网

Python Dash应用程序没有';t显示基于下拉选择的绘图

Python Dash应用程序没有';t显示基于下拉选择的绘图,python,pandas,plotly,plotly-dash,Python,Pandas,Plotly,Plotly Dash,我试图在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 fro

我试图在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)
导入破折号
将仪表板核心组件作为dcc导入
将dash_html_组件导入为html
从dash.dependencies导入输入,输出
导入plotly.graph_对象作为go
从plotly.offline导入iplot
作为pd进口熊猫
将numpy作为np导入
#对列表数据进行初始化。
数据={'Name':['Nick hospital','Nick hospital','Nick hospital','Krish hospital','Krish hospital','Krish hospital'],
“NAR_forms_used”:[2,1,2,2,3]
}
#创建数据帧
df=pd.DataFrame(数据帧
hosp_list=df[“Name”].unique()
外部_样式表=['https://codepen.io/chriddyp/pen/bWLwgP.css']
app=dash.dash(名称,外部样式表=外部样式表)
app.layout=html.Div([
dcc.Graph(id='Graph'),
dcc.下拉列表(
id=“Hosp_列表”,
选项=[{“标签”:i,“值”:i}用于hosp_列表中的i],
多重=真,
值=列表(),
)
])
@app.callback(
输出('图形','图形'),
[输入(‘Hosp_列表’、‘值’)]
def更新图(Hosp清单):
#获取每个NAR类型的计数
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()
#管理NAR类型(谁知道,随着时间的推移,可能会有更多类型?)
nars=df_nar['nar_forms_used'].unique()
nars=nars.tolist()
nars.sort(反向=假)
#设图
图=go.Figure()
#每个NAR类型添加一个跟踪,并显示每个医院的计数
对于nar中的nar:
#按NAR类型划分的数据帧子集
df_ply=df_nar[df_nar['nar_forms_used']==nar]
#添加跟踪
图添加轨迹(go.Bar(x=df_ply['Name'],y=df_ply['NAR count'],Name='NAR Type='+str(NAR)))
#让这个数字更形象一点
图:更新布局图(标题为每家医院的NAR),
yaxis=dict(title='NAR类型计数'),
xaxis=dict(title='Hospital',
)
)
图2(图3)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app.run_服务器(debug=True)

请帮助我修复该应用程序,使其能够运行。最终产品是一个应用程序,在该应用程序中,根据下拉列表中的医院选择显示图表

在更新图的末尾,添加返回图您得到了什么错误?在更新图的末尾,添加返回图您得到了什么错误?