簡體   English   中英

添加下拉菜單以繪制表達樹狀圖

[英]Add dropdown menu to plotly express treemap

我目前正在嘗試向我的樹狀圖添加下拉菜單

我正在使用的代碼:

import pandas as pd
import plotly.express as px

fig = px.treemap(df, 
                 path=['RuleName','RuleNumber','ParaInvolved',"CreationP","MAjP"],
                 color='Somme',
                 hover_data=["RuleDecision","RuleMAJ"],
                 color_continuous_scale='RdBu')
    
fig.show()

我面臨的問題是,在我的“RuleName”列中,我有 151 個不同的值(但總共有 1300 行),這就是為什么我試圖添加一個按鈕,允許自己選擇我想要繪制樹狀圖的 RuleName 值. 現在我正在使用一種野蠻的方法,包括通過每個 RuleName 值過濾我的數據框,這導致我獲得 151 個不同的樹狀圖。 我在該網站或任何其他網站上找不到任何解決方案。

謝謝你的幫助

在這里,我基本上使用了這個答案中的相同邏輯,但我使用px.treemap(...).data[0]來生成跟蹤而不是go

import plotly.express as px
import plotly.graph_objects as go
df = px.data.tips()

# We have a list for every day
# In your case will be gropuby('RuleName')
# here for every element d
# d[0] is the name(key) and d[1] is the dataframe
dfs = list(df.groupby("day"))

first_title = dfs[0][0]
traces = []
buttons = []
for i,d in enumerate(dfs):
    visible = [False] * len(dfs)
    visible[i] = True
    name = d[0]
    traces.append(
        px.treemap(d[1],
                   path=['day', 'time', 'sex'],
                   values='total_bill').update_traces(visible=True if i==0 else False).data[0]
    )
    buttons.append(dict(label=name,
                        method="update",
                        args=[{"visible":visible},
                              {"title":f"{name}"}]))

updatemenus = [{'active':0, "buttons":buttons}]

fig = go.Figure(data=traces,
                 layout=dict(updatemenus=updatemenus))
fig.update_layout(title=first_title, title_x=0.5)
fig.show()

在此處輸入圖片說明

暫無
暫無

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

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