Plotly en de Plotly-figure

Introductie tot datavisualisatie met Plotly in Python

Alex Scriven

Data Scientist

Wat is Plotly?

 

  • Een JavaScript-visualisatiebibliotheek
    • Geen zorgen: je hoeft geen JavaScript te kennen!
  • Plotly heeft een Python-wrapper

 

Plotly-logo

Introductie tot datavisualisatie met Plotly in Python

Waarom Plotly?

 

Plotly heeft unieke voordelen:

  • Snel en eenvoudig
  • Low-code/low-effort met plotly.express
  • (Indien gewenst) zeer aanpasbaar
  • Ingebouwde interactiviteit
Introductie tot datavisualisatie met Plotly in Python

Plotly-figures maken

 

Plotly-grafieken kun je maken:

  1. Met plotly.express voor snelle plots (px)
  2. Met plotly.graph_objects (go) voor meer controle

$$

📈 We gebruiken vooral px

Introductie tot datavisualisatie met Plotly in Python

Het belang van documentatie

Bewaar de links naar de kern‑documentatie!

  1. Interactieve documentatie

  2. Gedetailleerde referentiepagina voor specifieke plots

$$

Voor scatterplots:

Introductie tot datavisualisatie met Plotly in Python

Onze figure maken

  Een basis-Plotly-figure:

import plotly.express as px

days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] temperatures = [28, 27, 25, 31, 32, 35, 36]
fig = px.bar( x=days, y=temperatures, title="Temperatures of the week")
fig.show()
Introductie tot datavisualisatie met Plotly in Python

Onze figure onthuld

Staafdiagram van wekelijkse temperaturen

Introductie tot datavisualisatie met Plotly in Python

De Plotly-figure

 

Onderdelen van een Plotly-figure:

  • layout: dictionary die de stijl van de figure bepaalt
    • Eén layout per figure
  • data: lijst met dictionaries voor graftype en data zelf
    • Data + type = een trace. Er zijn >40 types!
    • Meerdere traces per grafiek mogelijk
  • frames: voor animaties (buiten deze cursus)
Introductie tot datavisualisatie met Plotly in Python

Binnenin een Plotly-figure

 

print(fig)
Figure({'data': [{'type': 'bar',
              'x': array(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'
              , 'Saturday','Sunday'], dtype=object),
              'y': {'bdata': 'HBsZHy AjJA==....'}],
    'layout': {'title': {'text': 'Temperatures of the week'}}})
Introductie tot datavisualisatie met Plotly in Python

Directe interactiviteit van Plotly

$$

  • Hover over punten
  • Extra interactieve knoppen

Hover-opties

Introductie tot datavisualisatie met Plotly in Python

Laten we oefenen!

Introductie tot datavisualisatie met Plotly in Python

Preparing Video For Download...