[英]Add dropdown menu to plotly express treemap
I am currently trying to add a dropdown menu to my treemap plot我目前正在尝试向我的树状图添加下拉菜单
The code I am using :我正在使用的代码:
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()
The problem I am facing is that in my column "RuleName" I have 151 different values (but 1300 rows in total), that's why I'm trying to add a button allowing myself to chose for what RuleName value I want to plot my treemap.我面临的问题是,在我的“RuleName”列中,我有 151 个不同的值(但总共有 1300 行),这就是为什么我试图添加一个按钮,允许自己选择我想要绘制树状图的 RuleName 值. For now I am using a barbaric method consisting in filtering my dataframe by each RuleName value, which lead me to get 151 different treemap.现在我正在使用一种野蛮的方法,包括通过每个 RuleName 值过滤我的数据框,这导致我获得 151 个不同的树状图。 I don't find any solution on that website or any other.我在该网站或任何其他网站上找不到任何解决方案。
Thanks for your help谢谢你的帮助
Here I'm basically using the same logic from this answer but I use px.treemap(...).data[0]
to produce the traces instead of go
.在这里,我基本上使用了这个答案中的相同逻辑,但我使用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.