0

Hi I am able to add one dropdown in plotly graph plot. But wondering how one can add two and more dropdown to update accordingly.

This is code and has three subplots. By selecting from dropdown plots will update. I just want to add one more dropdown which can change height or width of plot.

Note: Without plotly-dash

import plotly.graph_objs as go
from plotly import subplots 
trace0 = go.Scatter(x=[1, 2, 3], y=[4, 5, 6], mode="lines+markers")
trace1 = go.Scatter(x=[1, 2, 3], y=[5, 4, 6], mode="lines+markers")
trace2 = go.Scatter(x=[1, 2, 3], y=[6, 5, 4], mode="lines+markers")
fig = subplots.make_subplots(rows=3,cols=1,shared_xaxes=True,horizontal_spacing=0.5)
fig.add_trace(trace0, 1, 1)
fig.add_trace(trace1, 1, 1)
fig.add_trace(trace2, 1, 1)
fig.add_trace(trace0, 2, 1)
fig.add_trace(trace0, 3, 1)

update_menus = [go.layout.Updatemenu(
        active=0,
        buttons=list(
            [dict(label = 'All',
                  method = 'update',
                  args = [{'visible': [True, True, True,True,True]},
                          {'title': 'all',
                           'showlegend':True}]),
             dict(label = 'First',
                  method = 'update',
                  args = [{'visible': [True, False, False,True,True]}, # the index of True aligns with the indices of plot traces
                          {'title': 'first',
                           'showlegend':True}]),
             dict(label = 'Second',
                  method = 'update',
                  args = [{'visible': [False, True, False,True,True]},
                          {'title': 'second',
                           'showlegend':True}]),
             dict(label = 'Third',
                  method = 'update',
                  args = [{'visible': [False, False, True, False,False]},
                          {'title': 'third',
                           'showlegend':True}]),
            ])
        )
               ]           

fig.update_layout(updatemenus=update_menus)

fig.show()
2
  • Did my answer solve your problem after update based on your request? Commented Nov 24, 2022 at 21:07
  • Yes it solve my solution but I have another problem When i select 1000 or 1500 from dropdown, position of button is also changed. Can we make both button at fix position? Commented Dec 1, 2022 at 2:52

1 Answer 1

1

You will add it to the dictionary as the first one. The vertical position of y is calculated from the middle (yanchor="middle") because the height varies. You will also use method="relayout" because you change the height:

import plotly.graph_objs as go
from plotly import subplots 
trace0 = go.Scatter(x=[1, 2, 3], y=[4, 5, 6], mode="lines+markers")
trace1 = go.Scatter(x=[1, 2, 3], y=[5, 4, 6], mode="lines+markers")
trace2 = go.Scatter(x=[1, 2, 3], y=[6, 5, 4], mode="lines+markers")
fig = subplots.make_subplots(rows=3,cols=1,shared_xaxes=True, specs=[[{}], [{}], [{}]])
fig.add_trace(trace0, 1, 1)
fig.add_trace(trace1, 1, 1)
fig.add_trace(trace2, 1, 1)
fig.add_trace(trace0, 2, 1)
fig.add_trace(trace0, 3, 1)

fig.update_layout(height=550,margin={"t":10},
    updatemenus=[
        dict(
            buttons=list([
                dict(label = 'All',
                  method = 'update',
                  args = [{'visible': [True, True, True,True,True]},
                          {'title': 'all',
                           'showlegend':True}]),
             dict(label = 'First',
                  method = 'update',
                  args = [{'visible': [True, False, False,True,True]}, 
                          {'title': 'first',
                           'showlegend':True}]),
             dict(label = 'Second',
                  method = 'update',
                  args = [{'visible': [False, True, False,True,True]},
                          {'title': 'second',
                           'showlegend':True}]),
             dict(label = 'Third',
                  method = 'update',
                  args = [{'visible': [False, False, True, False,False]},
                          {'title': 'third',
                           'showlegend':True}]),
            ]),
            direction="down",
            showactive=True,
            x=0.18,
            y=1.1,
            xanchor="left",
            yanchor="middle"
        ),
        dict(
            buttons=list([
                dict(
                    args=["height", 550],
                    label="Default",
                    method="relayout"
                ),
                dict(
                    args=["height", 1200],
                    label="1500",
                    method="relayout"
                ),
                dict(
                    args=["height", 1000],
                    label="1000",
                    method="relayout"
                ),
                dict(
                    args=["height", 800],
                    label="850",
                    method="relayout"
                )
            ]),
            direction="down",
            showactive=True,
            x=0.4,
            xanchor="left",
            y=1.1,
            yanchor="middle"
        )
    ],
    
)

Output enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

When i select 1000 or 1500 from dropdown, position of button is also changed. Can we make both button at fix position?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.