lundi 27 avril 2020

Plotly dropdown selection does not update plots correctly

Good day,

I want to create an interactive plot with selection possibility to save a standalone HTML file, thus I do not want to use dash.

The goal is to display housing price trends with the possibility to make a selection for region and number of rooms. The problem is that once I change the selection, all plots are still active without correctly updating. Could someone help to solve this issue? I have made the plotting in my own language (Lithuanian), I translated the variable names in the code for convenience.

Current output: Output example, with button not working corretly

def compare_elements_with_single_value(all_list, value):
    all_comparison = []

    for elem in all_list:
        comp = elem == value
        all_comparison.append(comp)

    return(all_comparison) #e.g. [False, False, ...., True]

df = df.sort_values(["Region_title", "Room number", "Year_quarter"])

fig = go.Figure()

region_list = df['Region_title'].unique()
room_number_list = df['Room number'].unique()

all_list = []

for region in region_list:
    for room_number in room_number_list:
        all_list.append(str(region) + " Room number " + str(room_number)) 

for region in region_list:
    for room_number in room_number_list:

        region_room_number = str(region) + " Room number " + str(room_number)

        fig.add_trace(go.Scatter(
            x = df[(df['Region_title'] == region)
                            & (df['Room number'] == room_number)]['Year_quarter'],
            y = df[(df['Region_title'] == region)
                            & (df['Room number'] == room_number)]['price'],
            text = "Average price",
            hoverinfo = "x+y",
            name = region_room_number,
            mode ='lines+markers',
            marker={'size': 10, 'opacity': 0.5, 'line': {'width': 0.5, 'color': 'white'
                        }}))

updatemenu= []
buttons=[]
for region in region_list:
    for room_number in room_number_list:

        region_room_number = str(region) + " Room number " + str(room_number)

        buttons.append(dict(method='restyle',
                            label = str(region) + " Room number " + str(room_number),
                            args = [{'x':[df[(df['Region_title'] == region) 
                                           & (df['Room number'] == room_number)]['Metai_ketvirtis']]},
                                  {'y':[df[(df['Region_title'] == region)
                                           & (df['Room number'] == room_number)]['Price']]},
                                   {'visible': compare_elements_with_single_value(all_list, region_room_number)}
                                   ]
                      ))

updatemenu=[]
your_menu=dict()
updatemenu.append(your_menu)
updatemenu[0]['buttons']=buttons
updatemenu[0]['direction']='down'
updatemenu[0]['showactive']=True

fig.update_layout(updatemenus=updatemenu,
                  showlegend=True,
                  yaxis_title="EUR / m2")
fig.show()

pyo.plot(fig, filename='Output/Flat price.html')



Aucun commentaire:

Enregistrer un commentaire