Python 使用破折号制作带有两列和日期列的交互式条形图?

Python 使用破折号制作带有两列和日期列的交互式条形图?,python,plotly,plotly-dash,Python,Plotly,Plotly Dash,我正在使用date列作为下拉列表的破折号绘制条形图。这将是一个交互式图形,如果用户更改日期下拉列表,该图形将发生变化 下面是示例数据帧: message Date Count Hi 01/08/19 10 Bye 01/08/19 20 GN 01/08/19 30 Hi 02/08/19 15 Bye 02/08/19 25 GN 02/08/19 35 下面是我当前的代码。我是第一次使用D

我正在使用date列作为下拉列表的破折号绘制条形图。这将是一个交互式图形,如果用户更改日期下拉列表,该图形将发生变化

下面是示例数据帧:

message  Date     Count
Hi       01/08/19   10
Bye      01/08/19   20
GN       01/08/19   30
Hi       02/08/19   15
Bye      02/08/19   25
GN       02/08/19   35
下面是我当前的代码。我是第一次使用Dash,所以有一些错误

app = dash.Dash()

app.layout = html.Div([

    dcc.Dropdown(
        id = 'first_dropdown',
        options = df.Date,
        placeholder='Select a Date'
    )
    html.Div(id='output-graph')

])

@app.callback(
    Output(component_id='output-graph', component_property='children'),
    [Input(component_id='first_dropdown', component_property='options')]
)


    return dcc.Graph(id = 'Bar_Plor', 
                  figure = {
                      'data' : [
                          {'x':df.message, 'y':df.count, 'type':'bar', 'name':'First Chart'}
                          ]
                      })


if __name__ == '__main__':
    app.run_server(port=4050, debug=True)

下面是一个适用于您的代码

import pandas as pd
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output



app = dash.Dash()
df = pd.DataFrame({'message': ['Hi', 'Bye', 'GN', 'Hi', 'Bye', 'GN'],
                   'Date': ['01/08/19', '01/08/19', '01/08/19', '02/08/19', '02/08/19', '02/08/19'],
                   'Count': [10, 20, 30, 15, 25, 35]})
app.layout = html.Div([

    dcc.Dropdown(
        id = 'first_dropdown',
        options = df.Date,
        placeholder='Select a Date'
    ),
    html.Div(id='output-graph')

    ])

@app.callback(
    Output(component_id='output-graph', component_property='children'),
    [Input(component_id='first_dropdown', component_property='options')]
    )

def update_output_div(input_value):
    return dcc.Graph(id = 'Bar_Plor',
                  figure = {
                      'data' : [
                          {'x':df.message, 'y':df.Count, 'type':'bar', 'name':'First Chart'}
                          ]
                      })


if __name__ == '__main__':
    app.run_server(port=4050, debug=True)


这对我有效

您的程序无法工作,您正在对不存在的函数使用装饰程序。也许你在浆糊里忘了什么?这对你有用吗。。我得到以下错误,调试器PIN:376-571-468*服务烧瓶应用程序“main”(延迟加载)*环境:生产警告:这是一个开发服务器。不要在生产部署中使用它。请改用生产WSGI服务器。*调试模式:在发生异常时,使用%tb查看完整回溯。系统出口:1
app = dash.Dash()

def createObject(x):
    options = []
    for i in x:
        options.append({
            'label': i,
            'value': i
        })
    return options

df = pd.DataFrame({'message1': ['Hi', 'Bye', 'GN', 'Hi', 'Bye', 'GN'],
                   'date1': ['01/08/19', '01/08/19', '01/08/19', '02/08/19', '02/08/19', '02/08/19'],
                   'count1': [10, 20, 30, 15, 25, 35]})

app.layout = html.Div([


    html.Br(),
    html.Br(),

    dcc.Dropdown(
        id = 'first_dropdown',
        options = createObject(df.date1.unique()),
        placeholder='Select a Date'
    ),

    html.Div(id='output-graph')
])

@app.callback(
    Output(component_id='output-graph', component_property='children'),
    [Input(component_id='first_dropdown', component_property='value')]
    )

def update_output_div(input_value):
    return dcc.Graph(id = 'Bar_Plot',
                  figure = {
                      'data' : [
                          {'x':df[df['date1']==input_value].message1, 'y':df[df['date1']==input_value].count1, 'type':'bar', 'name':'First Chart'}
                          ]
                      })

app.run_server(port=4050)