Kleur aanpassen

Introductie tot datavisualisatie met Plotly in Python

Alex Scriven

Data Scientist

Aanpassen in het algemeen

 

Zo pas je plots aan:

  1. Bij het maken, als er een argument is (zoals color)
  2. Na het maken met update_layout()
    • Neemt een dictionary
    • fig.update_layout({"title":{"text":"A New Title"}})
Introductie tot datavisualisatie met Plotly in Python

Waarom kleur aanpassen?

 

Kleur aanpassen helpt:

  1. Plots zien er top uit!
  2. Inzichten overbrengen
    • Kleur in deze scatterplot voegt een 3e dimensie toe.

   

Kleur als soort

Introductie tot datavisualisatie met Plotly in Python

Een beetje kleurtheorie

 

Computers gebruiken RGB-codering om kleuren te specificeren:

  • RGB = een 3-cijferige code (elk 0-255) die Rood, Groen, Blauw mengt
    • (0,0,255) is blauw en (255,255,0) is geel

$$

$$

Zie meer in dit artikel

tabel met RGB-kleurenvoorbeelden

Introductie tot datavisualisatie met Plotly in Python

Kleuren opgeven in plotly.express

 

  • color-argument (DataFrame-kolom)
    • Elke categorie krijgt automatisch een kleur
    • Bij numerieke kolommen wordt een kleurenschaal gebruikt

 

fig = px.bar(data_frame=student_scores, 
      x="student_name", 
      y="score", 
      title="Student Scores by Student"
      color="city")
fig.show()
Introductie tot datavisualisatie met Plotly in Python

Onze kleuren onthuld

De plot vóór:

Staafdiagram zonder kleuren

Onze plot na:

Staafdiagram met kleuren

Introductie tot datavisualisatie met Plotly in Python

Kleur bij univariate plots

 

plotly.express-argument color bij univariate (staaf, histogram) plots:

  • Histograms - gestapelde staven
  • Boxplots - maakt meerdere plots

Gestapeld histogram met kleur

boxplot naast elkaar met kleur

Introductie tot datavisualisatie met Plotly in Python

Specifieke kleuren in plotly.express

 

  • color_discrete_map: een dictionary die categoriewaarden aan kleuren koppelt
  • Basis­kleuren kun je ook als string geven, zoals "red", "green" enz.
Introductie tot datavisualisatie met Plotly in Python

Onze specifieke kleuren

$$

fig = px.bar(
    data_frame=student_scores, 
    x="student_name", y="score", 
    title="Student Scores by Student",

color_discrete_map={ "Melbourne": "rgb(0,0,128)", "Sydney": "rgb(235, 207, 52)"},
color="city")

 

Staafdiagram met opgegeven kleuren

Introductie tot datavisualisatie met Plotly in Python

Kleurenschalen in plotly.express

 

  • Enkele kleurenschaal (licht-naar-donker groen)

Groene kleurenschaal

  • Geleidelijk verloop (groen naar blauw)

$$

  • Argument color_continuous_scale voor kleurenschalen.

Groen-blauwe kleurenschaal

Introductie tot datavisualisatie met Plotly in Python

Ingebouwde kleurenschalen gebruiken

 

fig = px.bar(data_frame=weekly_temps, 
    x="day", y="temp", 
    color="temp",
    color_continuous_scale="inferno")
fig.show()

 

$$

 

Staafdiagram met inferno-kleurenschaal

Introductie tot datavisualisatie met Plotly in Python

Eigen kleurverloop maken

 

  • Aangepaste kleurenschaal (geel - oranje - rood)
my_scale=[("rgb(242, 238, 10)"),
          ("rgb(242, 95, 10)"), 
          ("rgb(255,0,0)")]

fig = px.bar(data_frame=weekly_temps, x="day", y="temp",
color_continuous_scale=my_scale, color="temp")

 

Staafdiagram met eigen kleurenschaal

Introductie tot datavisualisatie met Plotly in Python

Laten we oefenen!

Introductie tot datavisualisatie met Plotly in Python

Preparing Video For Download...