Templates and theming

Plotly · Advanced · 12 min

A shared template gives every chart in a product or report the same fonts, colors and layout without repeating settings.

A template is a figure-shaped bundle of defaults: a layout (fonts, colorway, background, axis styles) and per-trace-type data defaults. Built-ins live in plotly.io.templates: "plotly" (the default), "plotly_white", "plotly_dark", "simple_white", "ggplot2", "seaborn", "presentation" and "none".

Use one per figure with template="plotly_white", or globally:

import plotly.io as pio
pio.templates.default = "plotly_white"

Make your own by creating a go.layout.Template and registering it:

pio.templates["house"] = go.layout.Template(
    layout=dict(font_family="Inter", colorway=["#3F4F75", "#E8A33D"],
                title_x=0.02))
pio.templates.default = "plotly_white+house"

The + syntax composes templates left to right, so you only define the differences from a base.

Values set explicitly on a figure always win over the template. Templates only fill in what is not set, which is why colorway changes the default trace colors but not traces you colored yourself. Express functions also read px.defaults (px.defaults.template, px.defaults.width, px.defaults.color_discrete_sequence), handy for notebook-wide settings.

Template trace defaults are matched by trace type and cycle when there are several, which allows, for example, alternating marker symbols. A template can also include annotations such as a watermark or a logo image that appear on every figure.

Example

import plotly.graph_objects as go
import plotly.io as pio
import plotly.express as px

pio.templates["house"] = go.layout.Template(
    layout=dict(font=dict(family="Inter, sans-serif", size=13),
                colorway=["#3F4F75", "#E8A33D", "#4BA3A3"],
                title=dict(x=0.02),
                annotations=[dict(text="ACME analytics", showarrow=False,
                                  xref="paper", yref="paper", x=1, y=-0.18,
                                  font=dict(size=10, color="gray"))]))
pio.templates.default = "plotly_white+house"

fig = px.bar(x=["a", "b"], y=[3, 5], color=["a", "b"])
print(fig.data[0].marker.color)                 # #3F4F75 (from colorway)
print(fig.layout.template.layout.font.family)   # Inter, sans-serif
print(fig.layout.template.layout.plot_bgcolor)  # white (from plotly_white)

red = px.bar(x=["a"], y=[1], color_discrete_sequence=["red"])
print(red.data[0].marker.color)                 # red: explicit beats template

Remember

  • Templates bundle layout and trace defaults; built-ins are in pio.templates
  • Register a custom go.layout.Template and compose with base+house
  • Explicit figure settings always override template values

Watch out for

  • Expecting a new template to recolor traces whose colors were set explicitly.

Check yourself

  1. What does `pio.templates.default = 'plotly_white+house'` do?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: plotly.com/python/templates/