Reputation: 67
I would like the plotly graph title to change dynamically based on the selected value in the dropdown. The below code works fine to update the graph itself but it doesn't change the title.
I have also tried {'layout_title_text': 'Title 1'}
instead of {'layout':{'title': {'text': 'Title 1'}}}
but it didn't work either.
pio.renderers.default = 'notebook'
import plotly.graph_objects as go
from plotly.subplots import make_subplots
p = go.Figure()
y_values = ['label1', 'label2', 'label3']
x_values1 = [1,5,3]
x_values2 = [4,3,5]
p = p.add_trace(go.Bar(x = x_values1, y = y_values,
orientation='h'
))
updatemenus = [{'buttons': [{'method': 'update',
'label': 'Values 1',
'args': [{'x': [x_values1]},
{'layout':{'title': {'text': 'Title 1'}}}
]
},
{'method': 'update',
'label': 'Values 2',
'args': [{'x': [x_values2]},
{'layout':{'title': {'text': 'Title 2'}}}
]
}
],
'direction': 'down',
'showactive': True}]
p = p.update_layout(template = 'plotly_white',
updatemenus=updatemenus
)
p.show()
Upvotes: 1
Views: 3275
Reputation: 61214
Just drop 'layout'={}
in your setup:
'args': [{'x': [x_values1]},
{'title': {'text': 'Title 1'}}
]
},
Result:
And for the second option:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
p = go.Figure()
y_values = ['label1', 'label2', 'label3']
x_values1 = [1,5,3]
x_values2 = [4,3,5]
p = p.add_trace(go.Bar(x = x_values1, y = y_values,
orientation='h'
))
updatemenus = [{'buttons': [{'method': 'update',
'label': 'Values 1',
'args': [{'x': [x_values1]},
{'title': {'text': 'Title 1'}}
]
},
{'method': 'update',
'label': 'Values 2',
'args': [{'x': [x_values2]},
{'title': {'text': 'Title 2'}}
]
}
],
'direction': 'down',
'showactive': True}]
p = p.update_layout(template = 'plotly_white',
updatemenus=updatemenus
)
p.update_layout(title_text = 'Title 1')
p.show()
Upvotes: 4