简体   繁体   English

在Plotly Dash中访问跟踪状态

[英]Accessing state of traces in Plotly Dash

I'm using Plotly Dash to build a stacked bar chart with 3 trace values. 我正在使用Plotly Dash构建具有3个跟踪值的堆积条形图。

I'm trying to access the state of the trace values so that I can filter a dataframe and pass the resulting DF back to the plot, as opposed to simply hiding the traces on de-select. 我试图访问跟踪值的状态,以便可以过滤数据帧并将结果DF传递回绘图,而不是简单地在取消选择时隐藏跟踪。

for example, I have a dataframe : 例如,我有一个数据框:

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

My traces are 3 values (first, Second, Third) pertaining to a linear process where each value is a status marking the advancement of an item. 我的踪迹是与线性过程有关的3个值(第一,第二,第三),其中每个值都是标记项目进展的状态。

My intention is to be able to select statuses from further down the progression so only those items that have advanced to a certain step are plotted. 我的目的是能够从更进一步的过程中选择状态,以便仅绘制那些前进到特定步骤的项目。

As I select more advanced statuses in the trace legend, my plotted x-values should drop off since fewer advance that far, even though they all share the majority of the statuses 当我在轨迹图例中选择更高级的状态时,我绘制的x值应该下降,因为到目前为止,前进的次数较少,即使它们都共享大多数状态

The only solution I can think of is to make checkboxes for each trace value and use those inputs in a callback, but that seems redundant to the select/de-select traces functionality built in. 我能想到的唯一解决方案是为每个跟踪值创建复选框,并在回调中使用这些输入,但这似乎对内置的选择/取消选择跟踪功能是多余的。

You looking for something like that? 您在寻找类似的东西吗?

Code: 码:

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)

Output: 输出: 酒吧堆积

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM