Jurgita-ds
Jurgita-ds

Reputation: 67

How to update plotly graph title using dropdown?

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

Answers (1)

vestland
vestland

Reputation: 61214

Just drop 'layout'={} in your setup:

'args': [{'x': [x_values1]},
     {'title': {'text': 'Title 1'}}
       ]
      },

Result:

enter image description here

And for the second option:

enter image description here

Complete code:

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

Related Questions