Reputation: 47
I'm trying to update the colorbar title based on the button selection, wihtout success so far.
Here is my code :
import numpy as np
import plotly.graph_objects as go
df = pd.DataFrame(np.array([['Germany', 2, 3], ['United States', 5, 6], ["Italy", 8, 9]]),
columns=['country', 'data_a', 'data_b'])
fig = go.Figure(data=go.Choropleth(
locations = df['country'],
z = df['data_a'],
text = df['country'],
colorscale = 'Teal',
autocolorscale=False,
reversescale=False,
marker_line_color='black',
marker_line_width=0.3,
colorbar_title = '<b>data_a_title</b>',
locationmode ='country names'
))
#update map style
fig.update_layout(
geo=dict(
showframe=False,
showcoastlines=True,
projection_type='equirectangular'
)
)
#dropdown menu
button1 = dict(method = "update",
args = [{'z': [ df["data_a"] ] },
{"colorbar":{"title":"data_a_title"}}],
label = "data_a")
button2 = dict(method = "update",
args = [{'z': [ df["data_b"] ]},
{"colorbar_title":'data_b_title'}],
label = "data_b")
fig.update_layout(width=700,
coloraxis_colorbar_thickness=23,
updatemenus=[dict(y=0.2,
x=1,
xanchor='right',
yanchor='bottom',
active=0,
buttons=[button1, button2])
])
fig.show(config={"displayModeBar": False, "showTips": False})
As you can see, I tried with this :
{"colorbar":{"title":"data_a_title"}}
or
{"colorbar_title":'data_b_title'}
but none of them worked. Any idea about what I'm missing ?
Thank you!
Upvotes: 2
Views: 470
Reputation: 31236
for a chorpeth the colorbar title is part of the trace, not the layout. Hence your button definitions become:
#dropdown menu
button1 = dict(method = "update",
args = [{'z': [ df["data_a"] ], "colorbar":{"title":{"text":"data_a_title"} }},
{}],
label = "data_a")
button2 = dict(method = "update",
args = [{'z': [ df["data_b"] ], "colorbar":{"title":{"text":"data_b_title"} }},
{}],
label = "data_b")
Simplest way to understand this is to look at fig.data
and fig.layout
to understand the structure of the figure
Upvotes: 2