简体   繁体   中英

Plotly - Checkbox instead of dropdown selection

I am trying to use plotly to display a Line chart with sliders and a checkbox dropdown for filters.

My DataFrame (ct2) is like below:

Category Date Count
A 2022-06-01 123
B 2022-06-02 56
C 2022-06-03 42
C 2022-06-01 84
A 2022-06-05 32

My sliders are working, and my dropdown shows items, but when I select a Category, it does not update the chart.

I am facing two issues:

  1. My dropdown is not working (When I select a category from dropdown, the chart becomes empty)
  2. I would like to select multiple items from the checkbox instead of selecting just 1 item from a dropdown

My Code:

import plotly.graph_objects as go
import pandas as pd

fig = go.Figure()
for Category, group in ct2.groupby("Category"):
    fig.add_trace(go.Line(x= group["Date"], y= group["Count"], name= Category,
      hovertemplate="Category=%s<br>Date=%%{x}<br>Count=%%{y}<extra></extra>"% Category))
fig.update_layout(legend_title_text = "Category")
fig.update_xaxes(title_text="Date")
fig.update_yaxes(title_text="Count")

fig.update_layout(
    title_text="Time series with range slider and selectors"
)

fig.update_layout(
    xaxis=dict(
        rangeselector=dict(
            buttons=list([
                dict(count=2,
                     label="2m",
                     step="month",
                     stepmode="backward"),
                dict(count=6,
                     label="6m",
                     step="month",
                     stepmode="backward"),
                dict(count=1,
                     label="YTD",
                     step="year",
                     stepmode="todate"),
                dict(count=1,
                     label="1y",
                     step="year",
                     stepmode="backward"),
                dict(step="all")
            ])
        ),
        rangeslider=dict(
            visible=True
        ),
        type="date"
    )
)

fig.update_layout(fig_tmp.layout)

fig.update_layout(
    updatemenus=[
        {
            "buttons": [
                {
                    "label": c,
                    "method": "restyle",
                    "args": ["visible", [t.legendgroup == c for t in fig.data]],
                }
                for c in ct2["Category"].unique()
            ]
        }
    ]
)

fig.show()

The reason it was not displayed is that all display settings are false. You need to change the condition to true if the condition is met and false otherwise. The second question is, there are no checkboxes in the custom controls in plotly. The second question is that there are no checkboxes in plotly's custom controls. If you want to select multiple categories, you should add a button with multiple conditions. The code is as follows. {'label': 'A_C', 'method': '{'label': 'A_C', 'method': 'update','args': ['visible', [True, False, True]]}

import plotly.graph_objects as go
import pandas as pd

fig = go.Figure()
for Category, group in ct2.groupby("Category"):
    fig.add_trace(go.Scatter(x= group["Date"],
                             y= group["Count"],
                             name= Category,
                             hovertemplate="Category=%s<br>Date=%%{x}<br>Count=%%{y}<extra></extra>"% Category,
                             visible=True,
                            ))
    
fig.update_layout(legend_title_text = "Category")
fig.update_xaxes(title_text="Date")
fig.update_yaxes(title_text="Count")

fig.update_layout(
    title_text="Time series with range slider and selectors"
)

fig.update_layout(
    xaxis=dict(
        rangeselector=dict(
            buttons=list([
                dict(count=2,
                     label="2m",
                     step="month",
                     stepmode="backward"),
                dict(count=6,
                     label="6m",
                     step="month",
                     stepmode="backward"),
                dict(count=1,
                     label="YTD",
                     step="year",
                     stepmode="todate"),
                dict(count=1,
                     label="1y",
                     step="year",
                     stepmode="backward"),
                dict(step="all")
            ])
        ),
        rangeslider=dict(
            visible=True
        ),
        type="date"
    )
)


buttons = [{'args': [{'visible': [True, False, False]}], 'label': 'A', 'method': 'update'},
           {'args': [{'visible': [False, True, False]}], 'label': 'B', 'method': 'update'},
           {'args': [{'visible': [False, False, True]}], 'label': 'C', 'method': 'update'},
           {'args': [{'visible': [True, False, True]}], 'label': 'A_C', 'method': 'update'},
          ]

fig.update_layout(
    updatemenus=[
        {
            "buttons": buttons
        }
    ],
)

fig.show()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM