Overview

This notebook demonstrates how to apply built-in theme templates to plotly.py figures

In [1]:
import plotly.graph_objs as go
import plotly.io as pio
import pandas as pd

Load gapminder dataset for 1982

In [2]:
data = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv")
df_1982 = data[data['year']==1982]
df_1982 = df_1982.sort_values(['continent', 'country'])
df_1982.head()
Out[2]:
country year pop continent lifeExp gdpPercap
30 Algeria 1982 20033753.0 Africa 61.368 5745.160213
42 Angola 1982 7016384.0 Africa 39.942 2756.953672
126 Benin 1982 3641603.0 Africa 50.904 1277.897616
162 Botswana 1982 970347.0 Africa 61.484 4551.142150
198 Burkina Faso 1982 6634596.0 Africa 48.122 807.198586

Create a scatter plot of Life Expectancy and GDP per capita with markers sized by population

In [3]:
fig = go.FigureWidget()
for continent, continent_df in df_1982.groupby('continent'):
    fig.add_scatter(x=continent_df.lifeExp,
                    y=continent_df.gdpPercap,
                    marker={'size': continent_df['pop'].tolist(), 'sizemode': 'area', 'sizeref': 200000},
                    mode='markers',
                    text=continent_df.country,
                    name=continent)
fig.layout.yaxis.type = 'log'
fig.layout.xaxis.title = 'Life Expectancy'
fig.layout.yaxis.title = 'GDP per capita'
fig.layout.title = 'Life Expectancy and GDP per capita (1982)'
fig.layout.width = 900
fig.layout.height = 700
fig

Display the available themese

In [4]:
pio.templates
Out[4]:
Templates configuration
-----------------------
    Default template: None
    Available templates:
        ['ggplot2', 'seaborn', 'plotly', 'plotly_white', 'plotly_dark',
         'presentation', 'xgridoff']

Theme templates

Copy figure and update template

In [5]:
fig_seaborn = go.FigureWidget(fig)
fig_seaborn.layout.template = 'seaborn'
fig_seaborn
In [6]:
fig_ggplot2 = go.FigureWidget(fig)
fig_ggplot2.layout.template = 'ggplot2'
fig_ggplot2
In [7]:
fig_plotly = go.FigureWidget(fig)
fig_plotly.layout.template = 'plotly'
fig_plotly
In [8]:
fig_plotly_white = go.FigureWidget(fig)
fig_plotly_white.layout.template = 'plotly_white'
fig_plotly_white
In [9]:
fig_plotly_dark = go.FigureWidget(fig)
fig_plotly_dark.layout.template = 'plotly_dark'
fig_plotly_dark

Add on templates

Add on templates can be used on their own or combined with other templates to further customize the figure's appearance

The presentation template increases the size of the font, lines, and markers to make figures more visible from a distance. Here is presentation on its own

In [10]:
fig_presentation = go.FigureWidget(fig)
fig_presentation.layout.template = 'presentation'
fig_presentation

Templates can be combined by joining them together with a + character. Here the plotly template combined with with presentation

In [11]:
fig_plotly_presentation = go.FigureWidget(fig)
fig_plotly_presentation.layout.template = 'plotly+presentation'
fig_plotly_presentation

The xgridoff template can be used to remove the x-axis grid lines for a cleaner look

In [12]:
fig_plotly_presentation_xgridoff = go.FigureWidget(fig)
fig_plotly_presentation_xgridoff.layout.template = 'plotly+presentation+xgridoff'
fig_plotly_presentation_xgridoff

Export static images of each theme

Themed figures can be saved to static raster and vector images using the plotly.io.write_image function

In [13]:
for template in [None, 'ggplot2', 'seaborn',
                 'plotly', 'plotly_white', 'plotly_dark',
                 'presentation', 'plotly+presentation', 'plotly+presentation+xgridoff']:
    new_fig = go.Figure(fig)
    new_fig.layout.template = template
    pio.write_image(new_fig, 'images-gapminder/' + str(template) + '.png', scale=1.5)
    pio.write_image(new_fig, 'images-gapminder/' + str(template) + '.pdf')
In [ ]: