This notebook demonstrates how to apply built-in theme templates to plotly.py figures
import plotly.graph_objs as go
import plotly.io as pio
import pandas as pd
Load gapminder dataset for 1982
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()
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
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
FigureWidget({ 'data': [{'marker': {'size': [20033753.0, 7016384.0, 3641603.0, 970347.0, …
Display the available themese
pio.templates
Templates configuration ----------------------- Default template: None Available templates: ['ggplot2', 'seaborn', 'plotly', 'plotly_white', 'plotly_dark', 'presentation', 'xgridoff']
Copy figure and update template
fig_seaborn = go.FigureWidget(fig)
fig_seaborn.layout.template = 'seaborn'
fig_seaborn
FigureWidget({ 'data': [{'marker': {'size': [20033753.0, 7016384.0, 3641603.0, 970347.0, …
fig_ggplot2 = go.FigureWidget(fig)
fig_ggplot2.layout.template = 'ggplot2'
fig_ggplot2
FigureWidget({ 'data': [{'marker': {'size': [20033753.0, 7016384.0, 3641603.0, 970347.0, …
fig_plotly = go.FigureWidget(fig)
fig_plotly.layout.template = 'plotly'
fig_plotly
FigureWidget({ 'data': [{'marker': {'size': [20033753.0, 7016384.0, 3641603.0, 970347.0, …
fig_plotly_white = go.FigureWidget(fig)
fig_plotly_white.layout.template = 'plotly_white'
fig_plotly_white
FigureWidget({ 'data': [{'marker': {'size': [20033753.0, 7016384.0, 3641603.0, 970347.0, …
fig_plotly_dark = go.FigureWidget(fig)
fig_plotly_dark.layout.template = 'plotly_dark'
fig_plotly_dark
FigureWidget({ 'data': [{'marker': {'size': [20033753.0, 7016384.0, 3641603.0, 970347.0, …
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
fig_presentation = go.FigureWidget(fig)
fig_presentation.layout.template = 'presentation'
fig_presentation
FigureWidget({ 'data': [{'marker': {'size': [20033753.0, 7016384.0, 3641603.0, 970347.0, …
Templates can be combined by joining them together with a +
character. Here the plotly
template combined with with presentation
fig_plotly_presentation = go.FigureWidget(fig)
fig_plotly_presentation.layout.template = 'plotly+presentation'
fig_plotly_presentation
FigureWidget({ 'data': [{'marker': {'size': [20033753.0, 7016384.0, 3641603.0, 970347.0, …
The xgridoff
template can be used to remove the x-axis grid lines for a cleaner look
fig_plotly_presentation_xgridoff = go.FigureWidget(fig)
fig_plotly_presentation_xgridoff.layout.template = 'plotly+presentation+xgridoff'
fig_plotly_presentation_xgridoff
FigureWidget({ 'data': [{'marker': {'size': [20033753.0, 7016384.0, 3641603.0, 970347.0, …
Themed figures can be saved to static raster and vector images using the plotly.io.write_image
function
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')