Custom buttons

Introduction to Data Visualization with Plotly in Python

Alex Scriven

Data Scientist

What can custom buttons do?

 

Custom buttons can:

  • Update the data or layout elements of a plot

    • All of our update_layout() customizations could be in a button!
  • Assist with animations (beyond the scope of this course)

Introduction to Data Visualization with Plotly in Python

Custom buttons in Plotly

Added via an updatemenus argument with arguments:

  • type: buttons or dropdown
    • We will cover dropdowns later!
  • direction: Button orientation
    • Buttons can be beside (left) or on top of (down) each other
  • x/y: Floats to set the button positions
  • showactive: True/False to show the active (index of button) as pressed or not.
    • The active button is the currently selected one.
  • buttons: A list of button objects
Introduction to Data Visualization with Plotly in Python

Plot type with buttons

$$

fig = px.bar(
      data_frame=revenues, 
    x='Industry', y='Revenue',
    color='Industry')
fig.show()

Simple bar chart by industry

Introduction to Data Visualization with Plotly in Python

Button set up

 

my_buttons = [

{'label': "Bar plot",
'method': "update",
'args': [{"type": "bar"}]},
{'label': "scatterplot",
'method': "update",
'args': [{"type": "scatter", 'mode': 'markers'}]}
]
Introduction to Data Visualization with Plotly in Python

The args argument

One of the most confusing parts of Plotly!

  • Its structure is:

[{dictionary to send to data}, {dictionary to send to layout}]

Figure internal structure

Introduction to Data Visualization with Plotly in Python

Using args for layout updates

dir(fig.layout)

Figure layout elements

  • Update any of the arguments with 'args'
Introduction to Data Visualization with Plotly in Python

Using args for data updates

dir(fig.data[0])

Inside data layout

  • Some are familiar, and some will be helpful later
Introduction to Data Visualization with Plotly in Python

Button interactivity

$$

fig.update_layout({
  'updatemenus': [{'type': "buttons",

'direction': 'down', 'x': 1.3, 'y': 0.5,
'showactive': True, 'active': 0,
'buttons': my_buttons}] }) fig.show()

$$

Buttons

Introduction to Data Visualization with Plotly in Python

Let's practice!

Introduction to Data Visualization with Plotly in Python

Preparing Video For Download...