添加下拉菜单以图表示树状图

问题描述

我目前正在尝试向树图添加下拉菜单

我正在使用的代码

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个不同的树形图。我在该网站或其他任何网站上都找不到任何解决方案。

感谢您的帮助

解决方法

这里,我基本上使用与此answer相同的逻辑,但是我使用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()

enter image description here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...