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

How to create a button for the certain subplot

Now I have two subplots and five buttons. Two of buttons are used to switch the traces of the first subplot, and rest of buttons are planned to hide and show traces of the second subplot. Buttons among different subplots should be isolated. How should I do to acess my desired result?

Hi @YUANMeng,

If you only want to change trace visibility, each button should be defined

as follows:

my_button = dict(method='update', 
                 args=[{"visible": [True, True, False] }], 
                 label="Your Label " ) 

The list associated to the key 'visible' has the length equal to the total number of traces in fig.data,
and its elements are True or False, depending on whether you want to display or not the corresponding trace.

Here is an example of two subplots with a single trace in (1,1) and two traces in (1,2):

import plotly.graph_objects as go
from plotly.subplots import make_subplots

fig = make_subplots(
    rows=1, cols=2)

fig.add_trace(
    go.Bar(x=[1, 2, 3, 4],
           y=[7, 4, 5, 6],
           name="bar",
           visible=True
          ), 1,1)
fig.add_trace(
    go.Scatter(x=[1, 2, 3, 4],
               y=[4, 2, 5, 3],
               name="scatt1",
               visible=False,
               line_color="red"), 1, 2)
fig.add_trace(go.Scatter(x=[1,2,3,4], 
                         y =[1, 5, 3.5, 6], 
                         name='scatt2', 
                         visible=False, 
                         line_color='green'),1, 2)

button1 = dict(method='update', 
               args=[{"visible": [True, False, False] }], 
               label="cell 0" )
button2 = dict(method='update', 
               args=[{"visible": [False, True, True] }], 
               label="cell 1 " )   
button3 = dict(method='update', 
               args=[{"visible": [False, True, False] }], 
               label="cell 1 - 1st trace " ) 
    
button4 = dict(method='update', 
               args=[{"visible": [False, False, True] }], 
               label="cell 1-2nd trace " ) 
fig.update_layout(width=800, height=400,
                 updatemenus =[dict(type='buttons',
                                    buttons=[button1, button2, button3, button4],
                                    x=1.05,
                                    xanchor="left",
                                    y=1,
                                    yanchor="top")])

Thanks for ur reply!
I know the length of “visible” equal to the total number of traces. And I want to konw how to let the “visible” of a button just controls a subpolt’s traces. Take your code as a example, button2, 3 and 4 will not affect the trace of subplot1 whatever the visibility of it. I mean if trace0 is hidden, it still hidden, regardless of the execution of button2, 3 and 4.

@YUANMeng

Make it visible, i.e. set the first position in the visibility list, True. Any combination of True, False is permitted.