【问题标题】:Accessing state of traces in Plotly Dash在 Plotly Dash 中访问跟踪状态
【发布时间】:2019-01-29 21:59:26
【问题描述】:

我正在使用Plotly Dash 构建一个包含 3 个跟踪值的堆积条形图。

我正在尝试访问跟踪值的状态,以便我可以过滤数据框并将生成的 DF 传递回绘图,而不是简单地在取消选择时隐藏跟踪。

例如,我有一个数据框:

Item    Status    Value
1        First    2000
1        Second   3490
1        Third    542    
2        First    641    
2        Second    564        
3        First      10

我的痕迹是 3 个值(第一、第二、第三),它们与线性过程有关,其中每个值都是标记项目进展的状态。

我的目的是能够从更进一步的进程中选择状态,以便仅绘制那些已经前进到某个步骤的项目。

当我在跟踪图例中选择更高级的状态时,我绘制的 x 值应该会下降,因为到目前为止的进展较少,即使它们都共享大部分状态

我能想到的唯一解决方案是为每个跟踪值制作复选框并在回调中使用这些输入,但这对于内置的选择/取消选择跟踪功能来说似乎是多余的。

【问题讨论】:

    标签: python plotly plotly-dash


    【解决方案1】:

    您正在寻找类似的东西吗?

    代码:

    import dash
    from dash.dependencies import Output, Input
    import dash_core_components as dcc
    import dash_html_components as html
    import plotly
    import plotly.graph_objs as go
    import pandas as pd
    
    app = dash.Dash(__name__)
    
    df = pd.DataFrame({'Item': [1, 1, 1, 2, 2, 3],
                       'Status': ["First", "Second", "Third",
                                  "First", "Second", "First"],
                       'Value': [2000, 3490, 542, 641, 564, 10]})
    
    colors = {
        'background': '#111111',
        'background2': '#FF0',
        'text': '#7FDBFF'
    }
    df1 = df.loc[df["Status"] == "First"]
    df2 = df.loc[df["Status"] == "Second"]
    df3 = df.loc[df["Status"] == "Third"]
    trace1 = go.Bar(
                    x=df1["Item"],
                    y=df1["Value"],
                    name='First',
    )
    trace2 = go.Bar(
                    x=df2["Item"],
                    y=df2["Value"],
                    name='Second',
    )
    trace3 = go.Bar(
                    x=df3["Item"],
                    y=df3["Value"],
                    name='Third',
    )
    
    app.layout = html.Div(children=[
            html.Div([
                html.H5('Your Plot'),
                dcc.Graph(
                    id='cx1',
                    figure=go.Figure(data=[trace1, trace2, trace3],
                                     layout=go.Layout(barmode='stack')))],)])
    
    
    if __name__ == '__main__':
        app.run_server(debug=True)
    

    输出:

    【讨论】:

      猜你喜欢
      • 2012-11-16
      • 2023-01-03
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 1970-01-01
      • 2011-11-14
      • 1970-01-01
      • 2021-03-03
      相关资源
      最近更新 更多