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

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’))