Subplots

Introductie tot datavisualisatie met Plotly in Python

Alex Scriven

Data Scientist

Wat zijn subplots?

 

  • Subplots: 'mini-plots' in een raster

$$

  • Toon verschillende plottypes (zelfde data) of verschillende subsets

 

Subplots-overzicht

Introductie tot datavisualisatie met Plotly in Python

Opfrisser: traces

 

  • Plotly-figuren bevatten een lijst met traces — de data en het type

  • Toegankelijk via fig.data[0], fig.data[1]

    • Toe te voegen aan subplots met .add_trace()

$$

px_fig = px.scatter(df...)
print(px_fig)
Figure({'data': [trace1], 'layout': {...}})
Introductie tot datavisualisatie met Plotly in Python

Een 1x2-subplot maken

import plotly.express as px
from plotly.subplots import make_subplots

# Maak een subplot-raster
fig = make_subplots(rows=2, cols=1)

# Maak Plotly Express-figuren hist = px.histogram(revenues, x='Revenue') box = px.box(revenues, x='Revenue')
# Haal traces op en voeg toe aan subplots fig.add_trace(hist.data[0], row=1, col=1) fig.add_trace(box.data[0], row=2, col=1) fig.show()

 

Een finance-subplot

Introductie tot datavisualisatie met Plotly in Python

Subplots aanpassen

 

  • Geen algemene plottitel
  • Geen subplot-titels

$$

$$

$$

  • ✨ Maak het presentatiewaardig

 

eenvoudig finance-subplot

Introductie tot datavisualisatie met Plotly in Python

Subplot-titels

from plotly.subplots import make_subplots

fig = make_subplots(rows=2, cols=1,
    subplot_titles=[
    'Histogram van bedrijfsomzet', 
    'Boxplot van bedrijfsomzet'])

## Traces toevoegen (fig.add_trace())
fig.update_layout({'title': {'text': 'Plots van bedrijfsomzet', 'x': 0.5, 'y': 0.9}}) fig.show()

Subplot-titels gefixt

Meer opties in de documentatie

Introductie tot datavisualisatie met Plotly in Python

Gestapelde subplots

$$

fig = make_subplots(rows=3, cols=1, 
  subplot_titles=['Adelie-pinguïns'
  , 'Gentoopinguïns', 'Kinbandpinguïns'])

row_num = 1 for species in ['Adelie', 'Gentoo', 'Chinstrap']: # Filter data for this species df = penguins[penguins['Species'] == species]
scatter = px.scatter(df, x='Culmen Length (mm)' , y='Culmen Depth (mm)') # Voeg de trace toe aan de subplot fig.add_trace(scatter.data[0] , row=row_num, col=1) row_num +=1

Gestapelde pinguïn-subplots

Introductie tot datavisualisatie met Plotly in Python

Subplots met gedeelde assen

 

  • De x-as delen:
fig = make_subplots(
    rows=3, cols=1
    , shared_xaxes=True)

gedeelde x-as pinguïn-subplots

Introductie tot datavisualisatie met Plotly in Python

Laten we oefenen!

Introductie tot datavisualisatie met Plotly in Python

Preparing Video For Download...