Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 42 ยท Layout, Styling and Themes

Templates and Themes

Style every chart at once with Plotly templates: built-in themes like plotly_white and plotly_dark, combining templates, setting a default, and building and registering your own theme.

A template is a bundle of default styles: colors, fonts, backgrounds, grid lines, and defaults for each trace type. Every figure has one. When you set title_font_size yourself, it overrides the template; when you set nothing, the template decides. Templates are how you give a whole report, dashboard or website a consistent look without styling every figure by hand.

Plotly includes a set of built-in templates and lets you create your own.

Built-in templates

You choose a template by name, per figure or globally:

Python
fig = px.scatter(df, x="a", y="b", template="plotly_dark")   # one figure
fig.update_layout(template="simple_white")                    # change it later

import plotly.io as pio
pio.templates.default = "plotly_white"                        # all new figures
Template Look
plotly The default: light gray plot area, white grid lines
plotly_white White background, light grid lines
plotly_dark Dark background for dark-themed pages
ggplot2 Looks like R's ggplot2
seaborn Looks like a Seaborn plot
simple_white Clean: white, axis lines, no grid, good for papers
presentation Larger fonts and markers for slides
xgridoff, ygridoff, gridon Tweaks: turn grid lines off or on
none No styling at all
Python List and inspect the templates

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A template has two parts, just like a figure: a layout (backgrounds, fonts, colorway, axis styles) and data (defaults for each trace type: how bars, scatter markers, heatmaps look). When a figure is drawn, plotly.js starts from the template and applies your explicit settings on top.

See the effect

The following example shows the same data with a light and a dark treatment, using the layout properties that the templates set (the browser example does not have Python templates, so we apply the settings by hand):

A dark theme by hand

If your website has a dark mode, create the chart with a matching template so that it does not look like a white rectangle on a dark page. You can also set transparent backgrounds (paper_bgcolor="rgba(0,0,0,0)") and let the page show through.

Combining templates

Join template names with +. Later templates override earlier ones, so each only needs to change a few things:

Python
fig.update_layout(template="plotly_white+presentation")
fig.update_layout(template="simple_white+xgridoff")

presentation adds bigger text and thicker lines on top of a clean base. gridon and xgridoff are small tweaks designed to be added to others.

Building your own template

A custom template is a go.layout.Template object. Set layout properties for the global look and data defaults for specific trace types. Register it under a name, then use it like any built-in one:

Python Create and register a brand template

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the nested structure: layout holds everything global, and data.scatter is a list of trace defaults (the template can have more than one, cycled in order). With the template registered, one line, pio.templates.default = "brand", restyles every figure afterwards.

Where defaults come from

When you wonder why a chart looks the way it does, check in this order:

  1. Explicit settings on the trace or in fig.update_layout(...).
  2. The figure's template (fig.layout.template).
  3. plotly.js built-in defaults.

Printing fig.layout.template.layout shows what the template contributes. To remove the template's influence entirely for a figure, use template="none". To get a figure whose JSON includes no template (smaller, and styled by the page's default), save it through fig.to_dict() and drop layout.template, as shown in the export lesson.

Tips for themes in applications

  • Define the theme once in a module, and call pio.templates.default = "brand" at start-up.
  • For Dash and Streamlit, use the template matching the app's light or dark mode (Streamlit applies a theme automatically unless you disable it with theme=None).
  • Keep text and grid contrast high, and keep your palette short and consistent.
  • Do not override template defaults in every figure; if you do it twice, move it into the template.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How do you make every new figure use plotly_white?

    plotly.io.templates.default sets the template used by figures created afterwards.
  2. 2. What does template="plotly_white+presentation" do?

    Templates can be combined with +, applied from left to right.
  3. 3. Which two parts does a template have?

    layout carries global styles and data carries defaults for each trace type.
  4. 4. Which wins when the template and an explicit update_layout disagree?

    Explicit settings always override template defaults.
  5. 5. Which template is the best starting point for slides?

    presentation enlarges fonts and markers so charts are readable when projected.

Summary

  • A template holds default layout and trace styling; every figure has one.
  • Choose built-ins (plotly_white, plotly_dark, simple_white, presentation, ...) per figure or with pio.templates.default.
  • Combine templates with +, and build your own with go.layout.Template, registered in pio.templates.
  • Explicit settings beat templates; use a template for what should be consistent everywhere.

Next: Axes, Ticks and Scales