Plotly Express:如何在交互式下拉过滤器中定义默认按钮

发布于 2025-01-20 17:25:58 字数 1971 浏览 5 评论 0原文

我制作了一个线图,并用太多的行进行整洁显示,因此,我需要能够选择一个分组变量(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文