Skip to content

Latest commit

 

History

History
141 lines (113 loc) · 4.67 KB

facet-plots.md

File metadata and controls

141 lines (113 loc) · 4.67 KB
jupyter
jupytext kernelspec language_info plotly
notebook_metadata_filter text_representation
all
extension format_name format_version jupytext_version
.md
markdown
1.2
1.3.4
display_name language name
Python 3
python
python3
codemirror_mode file_extension mimetype name nbconvert_exporter pygments_lexer version
name version
ipython
3
.py
text/x-python
python
python
ipython3
3.7.0
description display_as language layout name order page_type permalink redirect_from thumbnail
How to make Facet and Trellis Plots in Python with Plotly.
statistical
python
base
Facet and Trellis Plots
7
u-guide
python/facet-plots/
python/trellis-plots/
python/facet-trellis/
thumbnail/facet-trellis-thumbnail.jpg

Facet and Trellis Plots

Facet plots, also known as trellis plots or small multiples, are figures made up of multiple subplots which have the same set of axes, where each subplot shows a subset of the data. While it is straightforward to use plotly's subplot capabilities to make such figures, it's far easier to use the built-in facet_row and facet_col arguments in the various Plotly Express functions.

Scatter Plot Column Facets

import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", color="smoker", facet_col="sex")
fig.show()

Bar Chart Row Facets

import plotly.express as px
df = px.data.tips()
fig = px.bar(df, x="size", y="total_bill", color="sex", facet_row="smoker")
fig.show()

Wrapping Column Facets

When the facet dimension has a large number of unique values, it is possible to wrap columns using the facet_col_wrap argument.

import plotly.express as px
df = px.data.gapminder()
fig = px.scatter(df, x='gdpPercap', y='lifeExp', color='continent', size='pop',
                facet_col='year', facet_col_wrap=4)
fig.show()

Histogram Facet Grids

import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x="total_bill", y="tip", color="sex", facet_row="time", facet_col="day",
       category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "time": ["Lunch", "Dinner"]})
fig.show()

Facets With Independent Axes

By default, facet axes are linked together: zooming inside one of the facets will also zoom in the other facets. You can disable this behaviour when you use facet_row only, by disabling matches on the Y axes, or when using facet_col only, by disabling matches on the X axes. It is not recommended to use this approach when using facet_row and facet_col together, as in this case it becomes very hard to understand the labelling of axes and grid lines.

import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", color='sex', facet_row="day")
fig.update_yaxes(matches=None)
fig.show()
import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", color='sex', facet_col="day")
fig.update_xaxes(matches=None)
fig.show()

Customize Subplot Figure Titles

Since subplot figure titles are annotations, you can use the for_each_annotation function to customize them.

In the following example, we pass a lambda function to for_each_annotation in order to change the figure subplot titles from smoker=No and smoker=Yes to just No and Yes.

import plotly.express as px

fig = px.scatter(px.data.tips(), x="total_bill", y="tip", facet_col="smoker")
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
fig.show()

Synchronizing axes in subplots with matches

Using facet_col from plotly.express let zoom and pan each facet to the same range implicitly. However, if the subplots are created with make_subplots, the axis needs to be updated with matches parameter to update all the subplots accordingly.

Zoom in one trace below, to see the other subplots zoomed to the same x-axis range. To pan all the subplots, click and drag from the center of x-axis to the side:

import plotly.graph_objects as go
from plotly.subplots import make_subplots
import numpy as np

N = 20
x = np.linspace(0, 1, N)

fig = make_subplots(1, 3)
for i in range(1, 4):
    fig.add_trace(go.Scatter(x=x, y=np.random.random(N)), 1, i)
fig.update_xaxes(matches='x')
fig.show()