Subplots
1. Subplots
Let's learn how to create subplots in Plotly with Python.2. What are subplots?
Subplots are like mini-plots positioned in a grid arrangement. They're helpful in showing different plot types for the same data or the same plot type for different subsets. For example, you might have 4 plots arranged in a 2 by 2 grid like this. You can include as many subplots as you like, but keep in mind that each one shrinks the overall space.3. A reminder of traces
Remember discussing 'traces' in Chapter 1? Traces in Plotly Express are individual visual elements (lines, bars, points) in a figure. Each trace contains data and styling information. We can access them through the fig.data attribute, which returns a tuple of trace objects that we can index with fig.data[0], fig.data[1], etc., to extract or modify. To add traces to subplots, use the .add_trace() method. If we print a Plotly figure, we'll see this structure - traces and layout info neatly organized.4. Creating a 1x2 subplot
Let's rebuild our finance histogram and box plot as a 1x2 subplot using add trace. First, we import both plotly.express and make_subplots. We create a subplot grid with 2 rows and 1 column. Next, we create our individual plots using p x dot histogram and p x dot box. These are complete figure objects, each with its own data and layout. To build our subplot, we extract the trace from each p x figure using the data attribute and add it to our subplot grid. We specify the row and column position for each trace. And here's the result: a histogram on top, box plot below. Nice!5. Customizing subplots
The layout works, but we can improve the styling. There's no main title right now, and the subplots have no titles either. Let's make it presentation-worthy.6. Subplot titles
We'll start by setting subplot titles. The make_subplots function includes a subplot_titles argument, where we pass a list of titles - one for each subplot. Trace-adding remains the same, so we'll skip that here. To add a main title, we use update_layout with the title argument. We can also use the text, x, and y sub-arguments to adjust positioning. Nice work - that looks much better. And as always, check the documentation for more customization options.7. Stacked subplots
Let's redo our penguins scatterplot with subplots, splitting out the species for easier comparison. We create a subplot grid with one column and three rows, each with its own title. Then, we loop through each species, filter the data, and create a scatterplot for each one using Plotly Express. We add each scatterplot's trace to its corresponding subplot row. Looks good, but the x-axes aren't aligned, which makes comparison harder.8. Subplots with shared axes
We can improve the plot by making the x-axis shared between plots. When setting up the subplot grid, we add the shared_xaxes=True argument. The rest of the code stays the same. That's better; we can clearly see the difference in species now.9. Let's practice!
Let's practice creating subplots 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.