Customizing color

Introduction to Data Visualization with Plotly in Python

Alex Scriven

Data Scientist

Customization in general

 

How to customize plots:

  1. At plot creation, if an argument exists (like color)
  2. After plot is created use update_layout()
    • Takes a dictionary
    • fig.update_layout({"title":{"text":"A New Title"}})
Introduction to Data Visualization with Plotly in Python

Why customize color?

 

Customizing color can help:

  1. Make plots look awesome!
  2. Convey analytical insights
    • Color in this scatterplot adds a 3rd dimension.

   

Color as species

Introduction to Data Visualization with Plotly in Python

Some color theory

 

Computers use RGB encoding to specify colors:

  • RGB = A 3-digit code (each 0-255) mixing Red, Green, Blue
    • (0,0,255) is blue and (255,255,0) is yellow

$$

$$

See more in this article

table of RGB color examples

Introduction to Data Visualization with Plotly in Python

Specifying colors in plotly.express

 

  • color argument (DataFrame column)
    • Each category gets a color (automatically)
    • A color scale is used for numerical columns

 

fig = px.bar(data_frame=student_scores, 
      x="student_name", 
      y="score", 
      title="Student Scores by Student"
      color="city")
fig.show()
Introduction to Data Visualization with Plotly in Python

Our colors revealed

The plot before:

Bar chart without colors

Our plot after:

Bar chart with colors

Introduction to Data Visualization with Plotly in Python

Color with univariate plots

 

Using plotly.express color argument with univariate (bar, histogram) plots:

  • Histograms - stacked bars
  • Box plots - produces multiple plots

Stacked histogram with color

side by side boxplot with color

Introduction to Data Visualization with Plotly in Python

Specific colors in plotly.express

 

  • color_discrete_map: A dictionary that maps categorical values to colors
  • Can also express (basic) colors as strings such as "red", "green" etc.
Introduction to Data Visualization with Plotly in Python

Our specific colors

$$

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")

 

Bar chart with specified colors

Introduction to Data Visualization with Plotly in Python

Color scales in plotly.express

 

  • Single color scales (light to dark green)

Green color scale

  • Blended gradient (green into blue)

$$

  • color_continuous_scale argument for color scales.

Green blue color scale

Introduction to Data Visualization with Plotly in Python

Using built-in color scales

 

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

 

$$

 

Bar chart with inferno colorscale

Introduction to Data Visualization with Plotly in Python

Constructing our own color range

 

  • Custom color scale (yellow - orange - red)
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")

 

Bar char our own color scale

Introduction to Data Visualization with Plotly in Python

Let's practice!

Introduction to Data Visualization with Plotly in Python

Preparing Video For Download...