Plotly Templates and Themes: Built-In and Custom

Change the theme of a Plotly chart with built-in templates, set a default, combine and modify templates, build your own brand template, and reuse it across projects.

Change the theme of a Plotly chart with the template argument, for example px.bar(df, ..., template="plotly_white") or fig.update_layout(template="plotly_dark"). Set it for every chart with pio.templates.default = "plotly_white". To make your own theme, build a go.layout.Template and register it in pio.templates.

Quick Example

A custom template applied to made-up museum visitor numbers:

import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio

rng = np.random.default_rng(113)
galleries = ["Natural history", "Modern art", "Science hall"]
quarters = ["Q1", "Q2", "Q3", "Q4"]
visits = pd.DataFrame([
    {"gallery": g, "quarter": q, "visitors": int(base * season + rng.normal(0, 600))}
    for g, base in zip(galleries, [14_000, 9_500, 12_000])
    for q, season in zip(quarters, [0.8, 1.0, 1.3, 0.95])
])

pio.templates["museum"] = go.layout.Template(layout=dict(
    font=dict(family="Georgia, serif", size=14, color="#292524"),
    title=dict(x=0.02, font=dict(size=22)),
    colorway=["#9a3412", "#0f766e", "#ca8a04", "#4338ca"],
    paper_bgcolor="#faf7f0",
    plot_bgcolor="#faf7f0",
    xaxis=dict(showline=True, linecolor="#78716c"),
    yaxis=dict(gridcolor="#e7e0d2", zeroline=False),
    legend=dict(orientation="h", yanchor="bottom", y=1.02, x=0),
    margin=dict(t=110),
))

fig = px.bar(visits, x="quarter", y="visitors", color="gallery", barmode="group",
             title="Visitors by gallery", template="museum",
             labels={"quarter": "", "visitors": "Visitors"})
fig.update_layout(legend_title_text="")
fig.show()

Grouped Plotly bar chart of museum visitors by gallery and quarter using a custom template with a cream background, serif font and a legend on top

Colors, fonts, background, gridlines, and legend position all come from the template. The legend title is removed in the chart code because Plotly Express writes it onto the figure, where it beats the template. The code targets Plotly 6.x.

Built-In Templates

Print pio.templates to list them. Plotly 6 ships plotly (the default), plotly_white, plotly_dark, simple_white, ggplot2, seaborn, presentation, xgridoff, ygridoff, gridon, and none.

for name in ["plotly", "plotly_white", "simple_white", "ggplot2", "seaborn", "plotly_dark"]:
    fig = px.bar(visits, x="quarter", y="visitors", color="gallery", template=name,
                 title=f'template="{name}"', labels={"quarter": "", "visitors": ""})
    fig.update_layout(showlegend=False, margin=dict(l=50, r=20, t=50, b=30))
    fig.show()

The same stacked bar chart in six Plotly templates: plotly, plotly_white, simple_white, ggplot2, seaborn and plotly_dark

Template Look Good for
plotly Blue-gray plot area, white grid Exploring
plotly_white White with light grid Dashboards and reports
simple_white White, axis lines, no grid Papers and print
ggplot2, seaborn Imitate those libraries Matching other figures
plotly_dark Near-black background Dark mode apps
presentation Larger text and thicker marks Slides (combine it with another)
xgridoff, ygridoff, gridon Gridline switches Combining
none Almost no styling Starting from scratch

Set a Default Template

pio.templates.default applies to every figure created afterwards, from both Plotly Express and graph_objects. Figures you've already made don't change.

pio.templates.default = "simple_white"

fig = px.line(visits, x="quarter", y="visitors", color="gallery", markers=True,
              title="Uses the new default")
fig.show()

pio.templates.default = "plotly"   # back to the original

Line chart of museum visitors in the simple_white template

Plotly Express also has px.defaults.template, which affects only px functions.

Combine Templates with +

Join names with +. Where two templates set the same property, the one on the right wins.

fig = px.bar(visits, x="quarter", y="visitors", color="gallery", barmode="group",
             template="plotly_dark+presentation", title="Dark, with slide-sized text",
             labels={"quarter": "", "visitors": "Visitors"}, width=900, height=500)
fig.update_layout(legend=dict(orientation="h", yanchor="bottom", y=1.0, x=0, title_text=""),
                  margin=dict(l=100, t=110))
fig.show()

Grouped bar chart with a dark background and large presentation text

The same syntax works as a default: pio.templates.default = "plotly_white+museum".

Modify a Built-In Template

Copy the template before editing it. Changing pio.templates["plotly_white"] directly would affect every later chart that asks for it.

import copy

house = copy.deepcopy(pio.templates["plotly_white"])
house.layout.colorway = ["#0369a1", "#f97316", "#64748b"]
house.layout.font.family = "Verdana"
house.layout.hovermode = "x unified"
pio.templates["house"] = house

fig = px.line(visits, x="quarter", y="visitors", color="gallery", markers=True,
              template="house", title="plotly_white with three changes")
fig.show()

Line chart in a modified plotly_white template with blue, orange and gray lines

Default Trace Styles

layout styles the figure. data styles the traces, listed by trace type:

dots = go.layout.Template()
dots.data.scatter = [
    go.Scatter(mode="lines+markers", line=dict(width=4), marker=dict(size=12, symbol="circle")),
    go.Scatter(mode="lines+markers", line=dict(width=4, dash="dot"),
               marker=dict(size=12, symbol="diamond")),
]
pio.templates["dots"] = dots

wide = visits.pivot(index="quarter", columns="gallery", values="visitors")
fig = go.Figure([go.Scatter(x=wide.index, y=wide[g], name=g) for g in galleries])
fig.update_layout(template="plotly_white+dots", title="Styles cycle through the traces")
fig.show()

Three lines alternating between solid with circles and dotted with diamonds

With two entries, the first and third traces get the first style and the second gets the other. Anything set on a trace itself overrides the template, and Plotly Express sets line dashes and marker symbols on its traces, so this works best with graph_objects.

Add a Watermark or Source Note

Annotations in a template appear on every chart that uses it. Give each a name.

pio.templates["draft"] = go.layout.Template(layout=dict(annotations=[
    dict(name="watermark", text="DRAFT", textangle=-25, opacity=0.12,
         font=dict(size=110, color="#111827"), xref="paper", yref="paper",
         x=0.5, y=0.5, showarrow=False),
]))

fig = px.area(visits, x="quarter", y="visitors", color="gallery", template="plotly_white+draft",
              title="Every chart gets the watermark")
fig.show()

Stacked area chart with a large faint DRAFT watermark across it

To remove it from one chart, add fig.update_layout(annotations=[dict(templateitemname="watermark", visible=False)]).

Why Didn't the Colors Change?

Plotly Express picks trace colors when it creates the figure and writes them onto the traces. Switching templates afterwards changes the background and fonts but keeps the old colors.

before = px.bar(visits, x="quarter", y="visitors", color="gallery", barmode="group")
before.update_layout(template="museum")
print(before.data[0].marker.color)     # #636efa, still the "plotly" blue

after = px.bar(visits, x="quarter", y="visitors", color="gallery", barmode="group",
               template="museum", title="Pass template= to px to get its colors",
               labels={"quarter": "", "visitors": "Visitors"})
print(after.data[0].marker.color)      # #9a3412
after.show()

Grouped bar chart using the museum template colors

Pass template= to the px function, or set the default first. A color_discrete_sequence or color_discrete_map argument beats the template either way. graph_objects traces with no color of their own do follow a later template change.

Continuous color scales come from layout.colorscale.sequential and layout.colorscale.diverging in the template.

Save and Reuse a Template

pio.templates lasts for one Python session. Put the definition in a module you import, or save it as JSON:

import json
from pathlib import Path

Path("museum_template.json").write_text(
    json.dumps(pio.templates["museum"].to_plotly_json()))

loaded = go.layout.Template(json.loads(Path("museum_template.json").read_text()))
pio.templates["museum"] = loaded

Templates in Streamlit and Dash

  • Streamlit applies its own theme to Plotly charts, which replaces much of your template's styling. Use st.plotly_chart(fig, theme=None) to keep yours. See charts in Streamlit.
  • Dash uses whatever template your figures have. Set pio.templates.default at the top of the app. CSS on the page doesn't restyle the inside of a chart.

Similar Topics