Theming and templates in Python
Theming and templates with plotly with Python
Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Try Plotly Studio now.
Theming and templates¶
The Plotly Python library comes pre-loaded with several themes that you can get started using right away, and it also provides support for creating and registering your own themes.
Note on terminology: Theming generally refers to the process of defining default styles for visual elements. Themes in plotly are implemented using objects called templates. Templates are slightly more general than traditional themes because in addition to defining default styles, templates can pre-populate a figure with visual elements like annotations, shapes, images, and more. In the documentation we will refer to the overall process of defining default styles as theming, and when it comes to the plotly API we will talk about how themes are implemented using templates.
Using built-in themes¶
View available themes¶
To see information about the available themes and the current default theme, display the plotly.io.templates configuration object like this.
import plotly.io as pio
pio.templates
From this, we can see that the default theme is "plotly", and we can see the names of several additional themes that we can choose from.
Specifying themes in Plotly Express¶
All Plotly Express functions accept a template argument that can be set to the name of a registered theme (or to a Template object as discussed later in this section). Here is an example of using Plotly Express to build and display the same scatter plot with six different themes.
import plotly.express as px
df = px.data.gapminder()
df_2007 = df.query("year==2007")
for template in ["plotly", "plotly_white", "plotly_dark", "ggplot2", "seaborn", "simple_white", "none"]:
fig = px.scatter(df_2007,
x="gdpPercap", y="lifeExp", size="pop", color="continent",
log_x=True, size_max=60,
template=template, title="Gapminder 2007: '%s' theme" % template)
fig.show()
Specifying themes in graph object figures¶
The theme for a particular graph object figure can be specified by setting the template property of the figure's layout to the name of a registered theme (or to a Template object as discussed later in this section). Here is an example of constructing a surface plot and then displaying it with each of six themes.
import plotly.graph_objects as go
import pandas as pd
z_data = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/api_docs/mt_bruno_elevation.csv")
fig = go.Figure(
data=go.Surface(z=z_data.values),
layout=go.Layout(
title=dict(text="Mt Bruno Elevation"),
width=500,
height=500,
))
for template in ["plotly", "plotly_white", "plotly_dark", "ggplot2", "seaborn", "simple_white", "none"]:
fig.update_layout(template=template, title=dict(text="Mt Bruno Elevation: '%s' theme" % template))
fig.show()