添加下拉菜单以图表示树状图 [英] Add dropdown menu to plotly express treemap
问题描述
我目前正在尝试向树图添加一个下拉菜单
我正在使用的代码:
将pandas作为pd
导入plotly.express作为px
图= 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个不同的树形图。我在该网站或其他网站上找不到任何解决方案。
感谢您的帮助
这里,我基本上使用与此
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. 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. 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
.
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()
这篇关于添加下拉菜单以图表示树状图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!