Aangepaste knoppen

Introductie tot datavisualisatie met Plotly in Python

Alex Scriven

Data Scientist

Wat kunnen aangepaste knoppen?

 

Aangepaste knoppen kunnen:

  • Data of layout van een plot updaten

    • Al onze update_layout()-aanpassingen kunnen in een knop!
  • Helpen bij animaties (buiten de scope van deze cursus)

Introductie tot datavisualisatie met Plotly in Python

Aangepaste knoppen in Plotly

Toegevoegd via een updatemenus-argument met parameters:

  • type: buttons of dropdown
    • Dropdowns later!
  • direction: Knoporiëntatie
    • Knoppen kunnen naast elkaar (left) of boven elkaar (down) staan
  • x/y: Floats voor positie
  • showactive: True/False om active (index van knop) als ingedrukt te tonen of niet
    • De actieve knop is de huidige selectie.
  • buttons: Een lijst met button-objecten
Introductie tot datavisualisatie met Plotly in Python

Plottype met knoppen

$$

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

Eenvoudige staafgrafiek per branche

Introductie tot datavisualisatie met Plotly in Python

Knoppen instellen

 

my_buttons = [

{'label': "Staafdiagram",
'method': "update",
'args': [{"type": "bar"}]},
{'label': "scatterplot",
'method': "update",
'args': [{"type": "scatter", 'mode': 'markers'}]}
]
Introductie tot datavisualisatie met Plotly in Python

Het args-argument

Een van de meest verwarrende onderdelen van Plotly!

  • De structuur is:

[{dictionary naar data}, {dictionary naar layout}]

Interne structuur van Figure

Introductie tot datavisualisatie met Plotly in Python

Args gebruiken voor layout-updates

dir(fig.layout)

Figure layoutelementen

  • Werk elk argument bij met 'args'
Introductie tot datavisualisatie met Plotly in Python

Args gebruiken voor data-updates

dir(fig.data[0])

Binnen data-layout

  • Sommige herken je, andere zijn later handig
Introductie tot datavisualisatie met Plotly in Python

Knopinteractie

$$

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

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

$$

Knoppen

Introductie tot datavisualisatie met Plotly in Python

Laten we oefenen!

Introductie tot datavisualisatie met Plotly in Python

Preparing Video For Download...