Referring to layout shapes in updatemenus

All the dropdown (or slider) examples show modifications to data. However, shapes are placed in layout, so I don’t know how can I access them through updatemenus.

I.e., if I have two shapes and want to switch between them with a dropdown, I would like to write something like:

fig[‘layout’][‘updatemenus’] =
[{
x:-0.05, y:0.8,
buttons=[
{args:[‘shapes.visible’, [True, False]], label:‘1’, method:‘restyle’},
{args:[‘shapes.visible’, [False, True]], label:‘2’, method:‘restyle’}
]
}]

Obviously, it doesn’t work. So, is it possible to alter shapes like this?

Try:

fig['layout']['updatemenus'] = 
[{
  x:-0.05, y:0.8,
  buttons=[
     {args:['shapes[0].visible', false], method:'relayout'},
     {args:['shapes[1].visible', true], method:'relayout'}
  ]
}]

That would work to control each shape separately. Can I put two of them into one button?

Yes, I can:
dict(args=[{‘shapes[0].visible’: True, ‘shapes[1].visible’: False}], label=‘First circle’, method=‘relayout’),
dict(args=[{‘shapes[0].visible’: False, ‘shapes[1].visible’: True}], label=‘First circle’, method=‘relayout’),

2 Likes

The solution works perfectly but now when the data should also change with the button click (hence using the visible keyword as well), I cannot make it work, and I can’t understand why: ‘update’ should relayout and restyle, right?

{‘label’: ‘Prochaines’, ‘method’: ‘update’, ‘args’: [{‘shapes[0].visible’: False, ‘shapes[1].visible’: False, ‘visible’: [False, True, False]}]}

the api is a mess, well documented but 100x ways to do something but ppl just get confues

how to update a shape i previously added with:

        fig.add_shape(dict(  # vertical line
            type="line",
            x0=1, y0=2, x1=3, y1=4,
           line=dict(
                color="orange",
                width=1)))

i read entire https://plotly.com/python/shapes/ no help

assumes it is: fig.update_shapes(dict(x0=p_mid, y0=0, x1=p_mid, y1=max(amounts_),), selector=dict(type=‘line’))

I am facing the same issue.

For me, using update in combination with shapes does not work.

If I take the example provided in the documentation (https://plotly.com/python/custom-buttons/#relayout-button), I can use the buttons to toggle the shapes. However, when I change the method from relayout to update, nothing is updated.

I tried these options:

dict(label="Cluster 0", method="update", args=["shapes", cluster0])
dict(label="Cluster 0", method="update", args=[{"shapes": cluster0}])

In my production code, I am using this excerpt, which works just fine:

method='update',
args=[
    {'visible': some list},
    {"xaxis": some dictionary},
]

However, when adding a dictionary for shapes, the buttons do not provide any functionality.

For future readers: Apparently, the args parameter has a fixed interpretation of the entries provided in this list.

As described here, args accepts a list with two dictionaries and another list:

args = [
    { dictionary for updating traces },
    { dictionary for updating layouts }, 
    [ list of affected traces ]
]

In conclusion, for me it worked to change the update call to:

dict(label="Cluster 0", method="update", args=[{}, {"shapes": cluster0}])

It would be nice to add more details on this point to the reference.