Layering multiple plots

Introduction to Data Visualization with Plotly in Python

Alex Scriven

Data Scientist

What is plot layering?

  • Overlaying several plots within the same plot

$$

GDP layered plot

  • No separate grid positions
  • Use add_trace()

$$

$$

  • No separate grid positions
  • Use add_trace()
Introduction to Data Visualization with Plotly in Python

Why layer plots?

 

  • Accessing more customization
    • Example: layering multiple line graphs

$$

  • Displaying complementary plot types

$$

  • Using different plot types to direct attention

$$

  • Comparing data closely
Introduction to Data Visualization with Plotly in Python

Bar + line layered plot

 

  • Allows to compare exact values and the overall trend in one view

Layered plot example

Introduction to Data Visualization with Plotly in Python

GDP growth layered plot

from plotly.graph_objects import Figure

layered_fig = Figure()

bar_fig = px.bar(df, x='Date' , y='Quarterly Growth (%)')
line_fig = px.line(df, x='Date' , y='Rolling YTD Growth (%)' , color_discrete_sequence=['red'])
layered_fig = Figure(data= [*bar_fig.data, *line_fig.data]) layered_fig.show()

 

GDP layered plot

Introduction to Data Visualization with Plotly in Python

Adding more plots

 

# Code the same bar_fig, line_fig
line_fig2 = px.line(df, x='Date'
    , y='OECD Average GDP Growth (%)'
    , color_discrete_sequence=['green'])

layered_fig = Figure(data= [*bar_fig.data , *line_fig.data, *line_fig2.data]) layered_fig.show()

 

GDP layered plot

Introduction to Data Visualization with Plotly in Python

Adding more plots later

 

# Create with 2 figures
layered_fig = Figure(data=[*bar_fig.data, *line_fig.data])

# Add last line layered_fig.add_trace(line_fig2.data[0])
Introduction to Data Visualization with Plotly in Python

Let's practice!

Introduction to Data Visualization with Plotly in Python

Preparing Video For Download...