Python 无法将甘特图置为破折号

Python 无法将甘特图置为破折号,python,graph,plotly,plotly-dash,Python,Graph,Plotly,Plotly Dash,我精心策划了甘特的阴谋。但现在我想用下拉菜单把它放到dash上。由于我是dash新手,我确实生成了下拉菜单,但我正在努力将我的gannt绘图放在dash上,其中我的输入应该是下拉菜单中的值,输出应该是下拉菜单中选定值的线(图) 我非常感谢你的帮助。谢谢。我认为您在导入、变量名和格式方面不够小心。 首先从绘图的版本开始: import plotly.offline as py import plotly.figure_factory as ff import pandas as pd df =

我精心策划了甘特的阴谋。但现在我想用下拉菜单把它放到dash上。由于我是dash新手,我确实生成了下拉菜单,但我正在努力将我的gannt绘图放在dash上,其中我的输入应该是下拉菜单中的值,输出应该是下拉菜单中选定值的线(图)


我非常感谢你的帮助。谢谢。

我认为您在导入、变量名和格式方面不够小心。 首先从绘图的版本开始:

import plotly.offline as py
import plotly.figure_factory as ff
import pandas as pd

df = pd.DataFrame({'ObjectID': ['ITDM-1', 'ITDM-1', 'ITDM-1', 'ITDM-1',
                                'ITDM-10', 'ITDM-10', 'ITDM-10',
                                'ITDM-101', 'ITDM-101', 'ITDM-101'],
                   'Phase': ['phasezero', 'phaseone', 'phasetwo',
                             'phasethree', 'phasezero', 'phaseone',
                             'phasetwo', 'phasezero', 'phaseone', 'phasetwo'],
                   'StartDate': ['2016-12-1', '2017-3-22', '2017-8-21', '2017-9-21',
                                 '2016-12-1', '2016-12-5','2016-12-9', '2017-5-11',
                                 '2017-5-12', '2017-8-17'],
                   'EndDate': ['2017-5-22', '2017-8-21', '2017-9-21',  '2017-12-22',
                               '2017-2-5', '2017-4-9',  '2016-12-13', '2017-5-12',
                               '2017-8-17',  '2017-10-5']})


def gantt_fig(df):
    data = []

    for row in df.itertuples():
        data.append(dict(Task=str(row.Phase), Start=str(row.StartDate),     Finish=str(row.EndDate), Resource=str(row.ObjectID)))

    colors = ['rgb(0, 102, 204)', 'rgb(204, 0, 0)', 'rgb(0, 153, 0)']

    fig = ff.create_gantt(data, index_col='Resource', reverse_colors=True, show_colorbar=True, showgrid_x=True, title='Gantt Chart')
    fig['layout'].update( margin=dict(l=310))

    return fig


fig = gantt_fig(df)
py.iplot(fig)
从这里开始,您应该尝试将其翻译为破折号(再次)注意如何命名(与代码相比)

作为一般建议,我将首先编写一个函数,返回要用plotly进行绘图的
fig
。然后移动到dash,您可以检查下拉菜单是否正常工作,然后为fig

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.figure_factory as ff

import pandas as pd


def gantt_fig(df):
    data = []

    for row in df.itertuples():
        data.append(dict(Task=str(row.Phase), Start=str(row.StartDate),
                         Finish=str(row.EndDate), Resource=str(row.ObjectID)))

    colors = ['rgb(0, 102, 204)', 'rgb(204, 0, 0)', 'rgb(0, 153, 0)']

    fig = ff.create_gantt(data, index_col='Resource',
                          reverse_colors=True, show_colorbar=True,
                          showgrid_x=True, title='Gantt Chart')
    fig['layout'].update( margin=dict(l=310))

    return fig


df = pd.DataFrame({'ObjectID': ['ITDM-1', 'ITDM-1', 'ITDM-1', 'ITDM-1',
                                'ITDM-10', 'ITDM-10', 'ITDM-10',
                                'ITDM-101', 'ITDM-101', 'ITDM-101'],
                   'Phase': ['phasezero', 'phaseone', 'phasetwo', 'phasethree',
                             'phasezero', 'phaseone', 'phasetwo', 'phasezero',
                             'phaseone', 'phasetwo'],
                   'StartDate': ['2016-12-1', '2017-3-22', '2017-8-21', '2017-9-21',
                                 '2016-12-1', '2016-12-5', '2016-12-9', '2017-5-11',
                                 '2017-5-12', '2017-8-17'],
                   'EndDate': ['2017-5-22', '2017-8-21', '2017-9-21',  '2017-12-22',
                               '2017-2-5', '2017-4-9',  '2016-12-13', '2017-5-12',
                               '2017-8-17',  '2017-10-5']})

options = df['ObjectID'].unique()


app = dash.Dash()

app.layout = html.Div([html.H1('Gantt table'),
                       dcc.Dropdown(id='my-dropdown',
                                    options=[{'label': n, 'value': n}
                                             for n in options],
                                    value=options[0]),
                       dcc.Graph(id='display-selected-value')
                      ]
                     )



@app.callback(
    dash.dependencies.Output('display-selected-value', 'figure'),
    [dash.dependencies.Input('my-dropdown', 'value')])
def update_gantt(value):
    df2plot = df[df['ObjectID']==value].reset_index(drop=True)
    fig = gantt_fig(df2plot)
    return fig


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