2. Using Faceted Charts#

Faceted charts, also known as small multiples or trellis plots, are a popular data visualization technique consisting of splitting a dataset into subsets and displays them as individual charts. Each “facet”, or small chart, represents a combination of one or more categorical variables, allowing users to compare trends, distributions, or relationships across different categories in a structured way. For example, a faceted scatter plot might show the relationship between two continuous variables for each level of a categorical variable, such as sales performance across various regions.

The primary advantage of faceted charts is their ability to break down complex datasets into digestible parts, enabling comparisons while maintaining context. They are particularly useful when analyzing data segmented by categories, such as demographic groups, time periods, or geographic areas. By organizing the visualizations in a grid or matrix, faceted charts eliminate the need for complex color coding or overlaid graphics, reducing visual clutter and making patterns easier to discern. This approach is not only aesthetically clean but also analytically robust, as it highlights nuances and variations that might be obscured in aggregate data visualizations.

Getting ready#

For this recipe, we will use the stocks data set which is already available in Plotly

  1. Import the plotly.express module as px

import plotly.express as px
  1. Load the stocks data set from the Plotly express data submodule making sure to set the argument indexed to True

df = px.data.stocks(indexed=True)
df.head()
company GOOG AAPL AMZN FB NFLX MSFT
date
2018-01-01 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
2018-01-08 1.018172 1.011943 1.061881 0.959968 1.053526 1.015988
2018-01-15 1.032008 1.019771 1.053240 0.970243 1.049860 1.020524
2018-01-22 1.066783 0.980057 1.140676 1.016858 1.307681 1.066561
2018-01-29 1.008773 0.917143 1.163374 1.018357 1.273537 1.040708
  1. Visualise data with a simple line chart by using px.line to compare this chart with the faceted version

fig = px.line(df, width=800, height=500, title="Line plot showing all the stocks together")
fig.show()

How to do it#

  1. Import the plotly.express module as px

import plotly.express as px
  1. Make a faceted line chart by using the function line from Plotly express and setting the argument facet_col to "company"

fig = px.line(df, facet_col="company")
fig.show()
  1. Control the number of columns setting the argument facet_col_wrap. Try different configurations in order to find the best for your data

fig = px.line(df, facet_col="company", facet_col_wrap=3)
fig.show()
  1. When working with facets, adding elements to all consistently to all the subplots can help with communicating the message of the plot. For instance, here we are adding a horizontal line which highlights the baseline used for all the sub-plots. This helps to provide further context to the comparison

fig = px.line(df, facet_col="company", facet_col_wrap=3)

fig.add_hline(y=1, line_dash="dot",
              annotation_text="Jan 1, 2018 baseline",
              annotation_position="bottom right")

fig.show()
  1. Elements can also be added to specific subplots

fig = px.line(df, facet_col="company", facet_col_wrap=3)

fig.add_hline(y=1, line_dash="dot",
              annotation_text="Jan 1, 2018 baseline",
              annotation_position="bottom right")

fig.add_vrect(x0="2018-09-24", x1="2018-12-18", col=1,
              annotation_text="decline", annotation_position="top left",
              fillcolor="green", opacity=0.25, line_width=0)
fig.show()