'Dropdown Menu Histogram based on two columns - Plotly

I want to plot this figure based on the selection of a value from the first dropdown menu (year) and second drop menu (company). What I get is that it doesn't take in consideration of the year selected and only take in consideration the company value selector or the one around ( year without company value)
I tried the solution proposed here and other solutions but no luck. Dropdown bar chart (plotly) based on values column

dataframe:

date    company_name    field   count
0   2015    CM          EM      3
1   2015    P&G         EP      1
2   2017    CM          MS      2
3   2017    P&G         EM      5
4   2017    POSCO       MS      8
5   2020    CM          EM      6
6   2020    POSCO       MS      6
7   2020    POSCO       EP      5

here the code I wrote:

df = data.groupby(["date","company_name","field"])['product'].size().to_frame(name = 'count').reset_index()

fig = go.Figure()
fig.add_trace(go.Histogram(x=df.field,
                         y=df.count,
                         #colorscale=df.date,
                         visible=True)
             )

updatemenu = []
buttons = []
buttons1 = []

for col,col1 in zip(list(df.date.unique()),list(df.company_name.unique())):
    buttons.append(dict(method='restyle',
                        label=col,
                        visible=True,
                            args=[{'y':[df[(df.date==col)]["count"]],
                                   'x':[df[(df.date==col)].field],
                               'type':'histogram'}
                             
                             ],
                        )
                  )
    buttons1.append(dict(method='restyle',
                        label=col1,
                        visible=True,
                            args=[{'y':[df[(df.company_name==col1)]["count"]],
                                   'x':[df[(df.company_name==col1)].field],
                               'type':'histogram'}
                             
                             ],
                        )
                  )

updatemenu = []
your_menu = dict()
updatemenu.append(your_menu)
updatemenu.append(dict())
updatemenu[0]['buttons'] = buttons
updatemenu[0]['direction'] = 'down'
updatemenu[0]['showactive'] = True
updatemenu[0]['x'] = 0.1
updatemenu[1]['buttons'] = buttons1
updatemenu[1]['direction'] = 'down'
updatemenu[1]['showactive'] = True
updatemenu[1]['x'] = 0.5

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

result: enter image description here

I got the count for POSCO products but for all years not 2020. and if i select 2020, it shows the count product of all companies.

PS: Is there a way to color Histogram based on column like in Plotly Express color attribute within this case?

Thank you in advance!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source