Get startedGet started for free

Subplots

1. Subplots

Now we are going to look at Bokeh's layout functions - row, column, and gridplot. We will use these to display subplots, as well as reviewing how to customize the size of a figure.

2. Why use subplots?

There are many reasons to use subplots, such as to display a dashboard. Another use case is to visualize the distribution of multiple variables or to demonstrate how observations can vary by category.

3. Building a row

We use a row layout to show all subplots in a single row. This is useful if we have the same y-axis on all plots, as we can easily spot similarities and differences. If we want to present plots in a single row, we can import row from bokeh-dot-layouts. We create a source object for each subset of the data, then generate an individual figure for each subplot. Finally, we add glyphs to each subplot. To display the plot, we call row inside show, passing our two figures in the order we want them to appear.

4. Row subplot

There doesn't appear to be a significant difference for points and assists performance between the East and West conferences.

5. Column subplot

The column layout is best utilized when plots have the same x-axes. To build subplots in a column, we import column from bokeh-dot-layouts. To display them, we call column inside of show and pass our two figures. Here is what this looks like!

6. Building a gridplot

A gridplot displays figures in a grid pattern. To build a gridplot, we could continue to create each figure and add glyphs individually, but this becomes inefficient as we require more plots. Instead, we can use a for-loop, creating a gridplot showing points and assists for four different basketball positions. We import gridplot from bokeh-dot-layouts. Next, we create a list of the 4 positions we wish to plot. We store an empty list called plots, to which we will append each plot once created. We loop through positions, subsetting the dataset for the desired position and creating a source object. We create a figure, then add circle glyphs, setting legend label equal to the specific position. Finally, we append our plot to the plots list. We call show outside of the for-loop. Similar to row and column, we call gridplot inside of show, passing it the list of plots. Optionally, we can set the ncols argument, which determines the number of columns in our gridplot.

7. Gridplot

Here is our output, displaying assists and points for four basketball positions, including a legend for each subplot!

8. Customizing figure size

When creating subplots, screen space can get taken up quite quickly, so it's useful to modify the size of each figure. We do this using the width and height arguments when calling figure, passing numeric values representing how many pixels the width and height of the figure should consist of. Notice how the increased width of this figure creates separation between observations which were previously overlapping?

9. Let's practice!

Now we have a sense of how subplots can be beneficial, let's start building our own!

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.