Black Lives Matter. Please consider donating to Black Girls Code today.

Drop down menu change color Choropleth map

Hello, I am trying to app dropdown menus in my charts. The first one is Choropleth map.

After creating the map, i want to color the map according to selected input from drop down menu. Df includes two columns for ‘Confirmed’ and ‘Deaths’
The code is below and does not work.

fig = go.Figure(data=go.Choropleth(
locations=df[‘Country/Region’],
z = df[‘Confirmed’],
locationmode = ‘country names’ )

fig.update_layout(updatemenus=[
dict(
buttons=list([
dict(
args=[{‘visible’: [True, True]},
{‘z’:df[‘Confirmed’]}],
label=“Confirmed”,
method=“update”
),
dict(
args=[
{‘z’:df[‘Deaths’]},
],
label=“Deaths”,
method=“update”
)
]))
]

)

Thanks for your help !

@mhmtsrhn22

Here is an example on how to update the choroplethmapbox colorscale:

https://plotly.com/~empet/15605
and this is the corresponding plot: https://plotly.com/~empet/15603

Thanks for your response , it helped so much ! In case you want to help more, i have one question.

I created a pie plot and want to update labels and values. At first, i created two traces and used visible to just hide one of the traces. But i don’t want to use traces. Below is my code and just does not update graph but updates titles. What is the problem in the code ! Thank you in advance.

fig = go.Figure(go.Pie(labels=pie_chart_confirmed[“Country/Region”], values=pie_chart_confirmed[“Confirmed”], textinfo=‘label+percent’, name =“Confirmed”))

fig.update_layout(template=“plotly_dark”, title=dict(text=“confirmed”, x=0.5), showlegend=False,
updatemenus=list([dict(buttons=list([dict(label=“Confirmed”, method=“update”,
args=[{‘label’: pie_chart_confirmed[“Country/Region”],“value”: pie_chart_confirmed[“Confirmed”]},
{“title”: “confirmed”}]),
dict(label=“Deaths”, method=“update”,
args=[{‘label’: pie_chart_deaths[“Country/Region”], “value”: pie_chart_deaths[“Deaths”]},
{“title”: “deaths”}])]
))]))

@mhmtsrhn22

If you modify values and labels, i.e. data (values) , and a data attribute (labels) you have to call the restyle method, not update, because the update method modifies both data and layout.

Example:

labels1 = ["US", "China", "UK", "Iran"]
labels2 = [ "Brazil", "India", "Russia", "Korea"]

values1 = [18, 15, 13, 17]
values2 = [12, 16, 18, 10]



fig = go.Figure(go.Pie(labels=labels1, values=values1, name="A"))

button1 =  dict(method = "restyle",
              args = [{'labels': [labels1],
                       'values': [values1],
                        }    ], #i
              label = "Initial labels & values")
button2 =  dict(method = "restyle",
                args = [{'labels': [labels2],
                       'values': [values2],
                        }    ], 
              label = "New labels & values")
              
fig.update_layout(width=700,
                   height=700,
                   legend= dict(x=1.15, xanchor='right', itemsizing='trace'),
                   
                   updatemenus=[dict(active=0,
                                     buttons=[button1, button2])
                              ])