While colours are one of the most notable things we can change in Plotly, there are many more knobs we can turn to influence the visual aspects of our plots. In this post we explore how templates for Plotly work and what we need to create our own.
This post is part of my journey to learn Python. You find the code for this post in my PythonFriday repository on GitHub.
List the existing templates
Plotly organises the templates in the plotly.io module, that we can import as pio and ask for a list of known templates:
1 2 |
import plotly.io as pio pio.templates |
1 2 3 4 5 6 7 |
Templates configuration ----------------------- Default template: 'plotly' Available templates: ['ggplot2', 'seaborn', 'simple_white', 'plotly', 'plotly_white', 'plotly_dark', 'presentation', 'xgridoff', 'ygridoff', 'gridon', 'none'] |
You can find an example of each template in the official documentation.
Our plot with the default template
If we do not specify a template, Plotly uses the default template “plotly“:
Use a specific template
We can set a template for our plot with the template parameter:
1 2 3 4 5 6 |
fig = px.box(tips, x="time", y="total_bill", color="day", template='presentation') fig.show() |
This uses the presentation template with a larger font size to be visible from a greater distance:
Inspect a template
If we want to know the different settings in a template, we can fetch the template and check the contents of the layout property:
1 2 |
seaborn_template = pio.templates["seaborn"] seaborn_template.layout |
This gives us a dictionary with everything set in the seaborn template:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
Layout({ 'annotationdefaults': {'arrowcolor': 'rgb(67,103,167)'}, 'autotypenumbers': 'strict', 'coloraxis': {'colorbar': {'outlinewidth': 0, 'tickcolor': 'rgb(36,36,36)', 'ticklen': 8, 'ticks': 'outside', 'tickwidth': 2}}, ... 'font': {'color': 'rgb(36,36,36)'}, 'geo': {'bgcolor': 'white', 'lakecolor': 'white', 'landcolor': 'rgb(234,234,242)', 'showlakes': True, 'showland': True, 'subunitcolor': 'white'}, 'hoverlabel': {'align': 'left'}, 'hovermode': 'closest', 'paper_bgcolor': 'white', 'plot_bgcolor': 'rgb(234,234,242)', ... 'xaxis': {'automargin': True, 'gridcolor': 'white', 'linecolor': 'white', 'showgrid': True, 'ticks': '', 'title': {'standoff': 15}, 'zerolinecolor': 'white'}, 'yaxis': {'automargin': True, 'gridcolor': 'white', 'linecolor': 'white', 'showgrid': True, 'ticks': '', 'title': {'standoff': 15}, 'zerolinecolor': 'white'} }) |
Change the default template
If we want to change the default template, we can overwrite the setting in the plotly.io module:
1 |
pio.templates.default = "plotly_white" |
Create a custom template
We can create our own template by passing a dictionary with our template definition to the constructor of the Graph Objects Template class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
pio.templates["wip"] = go.layout.Template( layout_annotations=[ dict( name="WIP watermark", text="Work in Progress", textangle=-10, opacity=0.2, font=dict(color="black", size=100), xref="paper", yref="paper", x=0.5, y=0.5, showarrow=False, ) ] ) |
This template adds the text “Work in Progress” over our plot to mark the ones we are still developing:
1 2 3 4 5 6 7 |
fig = px.box(tips, x="time", y="total_bill", color="day", template='wip' ) fig.show() |
Combine templates
We can combine multiple templates and merge their settings but be aware that it may not make sense with all templates. To combine the seaborn template with our work in progress text, we can combine them with a + sign:
1 2 3 4 5 6 |
fig = px.box(tips, x="time", y="total_bill", color="day", template='wip+seaborn') fig.show() |
This uses the seaborn template and adds the “Work in Progress” text on top:
Next
With the templates we can style our plots in one go. The built-in templates cover a wide range of visualisations and are a good starting point for your own template, should you need something else.
Next week we explore a few additional ways Plotly offers us to customise our plots.