Plotly Express:如何在交互式下拉过滤器中定义默认按钮
我制作了一个线图,并用太多的行进行整洁显示,因此,我需要能够选择一个分组变量(label
),并且只需在给定类别中显示这些行即可。
例如,我将在下拉列表中单击a
,并且只有与类别a
显示的行相对应。在下文中,我提供了一个工作示例。对于所有类别,下拉菜单都可以正常工作,除非我想返回默认ash all
。
有人知道如何在交互式下拉过滤器中安装默认
按钮吗?
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
d = {
'num' : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
'label' : ['A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'C', 'C', 'D', 'D', 'D', 'D'],
'color' : ['red', 'blue', 'green', 'red', 'blue', 'green', 'blue', 'green', 'red', 'green', 'red', 'blue', 'red', 'blue', 'green', 'blue'],
'value' : [0.4, 0.2, 0.3, 0.6, 0.7, 0.4, 0.2, 0.4, 0.4, 0.2, 0.1, 0.3, 0.8, 0.4, 0.6, 0.5]
}
# Build dataframe
df = pd.DataFrame(data=d)
# Build dropdown Labels
labels = df["label"].unique()
buttonsLabels = [dict(label = "All",
method = "restyle",
visible=True,
args = [
{'x' : [df.num]},
{'y' : [df.values]},
{'color': [df.color]},
]
)]
for label in labels:
buttonsLabels.append(dict(label = label,
method = "restyle",
visible = True,
args = [
{'x' : [df.loc[df['label'] == label, "num"]]},
{'y' : [df.loc[df['label'] == label, "value"]]},
{'color' : [df.loc[df['label'] == label, "color"]]},
]
))
# Display figure
fig = px.line(x = df.num, y = df.value, color=df.color)
fig.update_layout(updatemenus = [
dict(buttons = buttonsLabels, showactive = True),
])
fig.show()
I made a line chart with way too many lines to neatly display, thus, I need to be able to select a grouping variable (label
) and have it just display the lines in a given category.
For example, I would click A
in the dropdown and have just the lines corresponding to category A
show up. In the following, I have provided a working example. The dropdown works perfectly fine for all categories except when I want to go back to default All
.
Does anybody know how to install a default
button in the interactive dropdown filter?
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
d = {
'num' : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
'label' : ['A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'C', 'C', 'D', 'D', 'D', 'D'],
'color' : ['red', 'blue', 'green', 'red', 'blue', 'green', 'blue', 'green', 'red', 'green', 'red', 'blue', 'red', 'blue', 'green', 'blue'],
'value' : [0.4, 0.2, 0.3, 0.6, 0.7, 0.4, 0.2, 0.4, 0.4, 0.2, 0.1, 0.3, 0.8, 0.4, 0.6, 0.5]
}
# Build dataframe
df = pd.DataFrame(data=d)
# Build dropdown Labels
labels = df["label"].unique()
buttonsLabels = [dict(label = "All",
method = "restyle",
visible=True,
args = [
{'x' : [df.num]},
{'y' : [df.values]},
{'color': [df.color]},
]
)]
for label in labels:
buttonsLabels.append(dict(label = label,
method = "restyle",
visible = True,
args = [
{'x' : [df.loc[df['label'] == label, "num"]]},
{'y' : [df.loc[df['label'] == label, "value"]]},
{'color' : [df.loc[df['label'] == label, "color"]]},
]
))
# Display figure
fig = px.line(x = df.num, y = df.value, color=df.color)
fig.update_layout(updatemenus = [
dict(buttons = buttonsLabels, showactive = True),
])
fig.show()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论