[Python] plotly histogram with filtering toolbar - python

Discussão em 'Python' iniciado por Stack, Fevereiro 26, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to generate a plotly histogram that can be filtered by values with a df column. Below, in dataset, I want to be able to filter the values in item to show the distribution of values for each unique value.

    However, all I am able to display is one single value. I can't change values to show the results of differing items.

    dataset = pd.DataFrame(
    {'item': ['A', 'A', 'B', 'C', 'D', 'A', 'A', 'B', 'C', 'D', 'A', 'A', 'B', 'C', 'D'],
    'value': [27, 33, 3, 26, 8, 27, 3, 13, 16, 2, 27, 17, 12, 6, 28],
    }
    )

    continuous_vars = ["item"]

    fig = go.Figure()

    fig.add_trace(
    go.Histogram(x = dataset.item,
    xbins=go.histogram.XBins(size=5), # Change the bin size
    marker=go.histogram.Marker(color="orange"), # Change the color
    )
    )

    buttons = []

    for col in continuous_vars:
    buttons.append(dict(method='restyle',
    label=col,
    visible=True,
    args=[{"x":[dataset[col]],
    "type":'histogram'}, [0]],
    )
    )

    updatemenu = []
    your_menu = dict()
    updatemenu.append(your_menu)

    updatemenu[0]['buttons'] = buttons
    updatemenu[0]['direction'] = 'down'
    updatemenu[0]['showactive'] = True

    fig.update_layout(showlegend=False, updatemenus=updatemenu)
    fig.show()

    Continue reading...

Compartilhe esta Página