簡體   English   中英

python plotly 破折號樹圖得到選擇的孩子 label

[英]python plotly dash treemap get selected child label

我正在嘗試在破折號內使用 plotly 樹形圖。 當用戶通過單擊選擇樹狀圖中的子組時,樹狀圖會放大所選部分。 有沒有辦法讓我獲得用戶的選擇並將其用作 Dash 回調的輸入?

例如,這里是 Dash 中樹圖的代碼:

import dash
import dash_core_components as dcc
import dash_html_components as html

import plotly.graph_objects as go

fig = go.Figure(go.Treemap(
    labels = ["Eve","Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
    parents = ["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve"],
    root_color="lightgrey"
))

fig.update_layout(margin = dict(t=50, l=25, r=25, b=25))

app = dash.Dash()
app.layout = html.Div([
    dcc.Graph(figure=fig)
])

app.run_server(debug=True, use_reloader=False)  # Turn off reloader if inside Jupyter

我想做的看起來像這樣:

@app.callback(
    dash.dependencies.Output('some_other_figure', 'figure'),
    [
     dash.dependencies.Input('treemap_child_selection', 'value'),
     ]
)
def update_other_figure(treemap_selection):
    pass

您可以在回調中使用dcc.GraphclickData屬性

clickData (dict; optional): 來自最新點擊事件的數據。 只讀。

@app.callback(
    dash.dependencies.Output("output", "children"),
    dash.dependencies.Input("graph", "clickData"),
)
def update_other_figure(click_data):
    print(click_data)
    # Do something with the data...

在您的示例中單擊Noam時, click_data的值是

{'points': [{'curveNumber': 0, 'pointNumber': 4, 'currentPath': 'Eve/Seth/', 'root': 'Eve', 'entry': 'Eve', 'percentRoot': 0.16666666666666666, 'percentEntry': 0.16666666666666666, 'percentParent': 0.5, 'parent': 'Seth', 'label': 'Noam'}]}

您可以從中檢索label的值並對其進行處理。

對我來說,理解字典結構需要一些時間。 我將語法放在這里以從 click_data 獲取正確的值

click_data["points"][0]["label"]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM