Get startedGet started for free

Layering multiple plots

1. Layering multiple plots

Let's learn how to layer multiple plots in the same visualization.

2. What is plot layering?

Unlike subplots, layered plots involve overlaying several plots within the same plot. We're not separating them into grid positions. To create this, we use add_trace(), which you just used in the previous video.

3. Why layer plots?

Layering plots is useful for a variety of reasons, such as: Accessing more customization options. For example, we could use add_trace to add and format multiple line graphs rather than just setting the color argument as before. Displaying complementary plot types without clutter, Different plot types can also direct attention compared with all data using the same plot type. In addition, layering makes it easier to compare data closely and visually than spreading it across separate subplots.

4. Bar + line layered plot

A common layered plot type is a bar chart with a line chart layered over the top. This allows us to compare exact values and the overall trend in one view.

5. GDP growth layered plot

Let's look at quarterly GDP growth in Australia, along with the rolling year-to-date growth. That is, the GDP growth for that quarter and the total growth for the year up to and including that quarter. We need to create a blank Figure object for layered plots to hold our visualization. Next, we build a bar chart to show quarterly GDP growth percentages. Then, we create a line chart for the rolling year-to-date GDP growth, also using Plotly Express. Finally, we combine both visualizations into a single figure by creating a new Figure with the data from both charts. The star allows us to unpack both figures into the new one. Now, we can see quarterly growth and rolling yearly growth side by side in one clean visualization.

6. Adding more plots

Suppose we want to add another line to show the OECD average GDP growth. We reuse the same code for the bar and line charts, which are omitted here for brevity. Then we create a new line chart for the OECD average and set its color to green. The change is quite small compared to what we know. We create the combined chart again, but this time, we include all three figures. The result is a plot with three traces: bars for quarterly growth, one line for rolling yearly growth, and another for the OECD average.

7. Adding more plots later

We can also add new traces after the figure is created. Let's say we've already layered two plots using Plotly Express. We can call add_trace() to add a third - like our green line.

8. Let's practice!

Let's practice building layered plots with Plotly in Python.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.