Select scatter plot marker size and colour values from dropdown

I’m trying to use the built in plotly widgets to select values for marker size and color in scatter and scatter_3d from plotly express. I can get this working using ipwidgets but then it resets the plot to the original orientation (3D). But when using updatemenus instead nothing changes at all.

Here’s an example code:

# stats is a pandas dataframe
 fig = px.scatter(stats, x='x', y='y',
                    color='cube num', size='Radius [mm] mean'                )
 fig.update_layout(scene_aspectmode='data')
 # fig.update_data(size='cube num')
 fig.update_layout(updatemenus=[
                     dict(
                         buttons=list([
                          dict(
                              args=[{'size':stats['cube num']},],
                              label='cube #',
                              method='update'
                          ),
                          dict(
                              args=[{'size':stats['Radius [mm] mean']},],
                              label='Average Radius',
                              method='update'
                          )         
                         ]),
                         showactive=True,
                         )                        
                      ]
                   
                     )
                  
 
 fig.show()

I’ve tried a number of variations to args and different methods.

@gawells
Your updatemenus definition has the following drawbacks:

  1. you called the update method, but here since your are updating only the trace attributes, you should set method='restyle'. (update method is called when you are re-setting both trace and layout attributes);
  2. to update marker size use the attribute marker.size, not just size;
  3. fig.data is a list of traces. In your case this list contains a single trace. When you are performing an update you must set the new values in a list (see https://chart-studio.plotly.com/~empet/15607/restyling-a-plotly-figure-via-a-dropdow/#/.
    Hence define the updatemenus as follows:
fig.update_layout(updatemenus=[
                     dict(
                         buttons=[
                          dict(
                              args=[{'marker.size': [stats['Radius [mm] mean']]}],
                              label='Average Radius',
                              method='update'
                          ),
                             dict(
                              args=[{'marker.size': [stats['cube num']]}],
                              label='cube #',
                              method='update'
                          )
                         ])
                        ]
                    )

Note that I reversed the buttons. The first one should set the size, as in fig.data[0] (just print(fig.data[0]).
Conclusion: to define updatemenus you must know how the corresponding fig.data is defined.