plotly.io.to_templated

plotly.io.to_templated(fig, skip=('title', 'text'))

Return a copy of a figure where all styling properties have been moved into the figure’s template. The template property of the resulting figure may then be used to set the default styling of other figures.

Parameters
  • fig – Figure object or dict representing a figure

  • skip – A collection of names of properties to skip when moving properties to the template. Defaults to (‘title’, ‘text’) so that the text of figure titles, axis titles, and annotations does not become part of the template

Examples

Imports

>>> import plotly.graph_objects as go
>>> import plotly.io as pio

Construct a figure with large courier text

>>> fig = go.Figure(layout={'title': 'Figure Title',
...                         'font': {'size': 20, 'family': 'Courier'},
...                         'template':"none"})
>>> fig 
Figure({
    'data': [],
    'layout': {'font': {'family': 'Courier', 'size': 20},
               'template': '...', 'title': {'text': 'Figure Title'}}
})

Convert to a figure with a template. Note how the ‘font’ properties have been moved into the template property.

>>> templated_fig = pio.to_templated(fig)
>>> templated_fig.layout.template
layout.Template({
    'layout': {'font': {'family': 'Courier', 'size': 20}}
})
>>> templated_fig
Figure({
    'data': [], 'layout': {'template': '...', 'title': {'text': 'Figure Title'}}
})

Next create a new figure with this template

>>> fig2 = go.Figure(layout={
...     'title': 'Figure 2 Title',
...     'template': templated_fig.layout.template})
>>> fig2.layout.template
layout.Template({
    'layout': {'font': {'family': 'Courier', 'size': 20}}
})

The default font in fig2 will now be size 20 Courier.

Next, register as a named template…

>>> pio.templates['large_courier'] = templated_fig.layout.template

and specify this template by name when constructing a figure.

>>> go.Figure(layout={
...     'title': 'Figure 3 Title',
...     'template': 'large_courier'}) 
Figure(...)

Finally, set this as the default template to be applied to all new figures

>>> pio.templates.default = 'large_courier'
>>> fig = go.Figure(layout={'title': 'Figure 4 Title'})
>>> fig.layout.template
layout.Template({
    'layout': {'font': {'family': 'Courier', 'size': 20}}
})
Returns

Return type

go.Figure