Creating Multiple Plots Using Bokeh in Python

Advertisements

In this post, we will look at how to make multiple plots at once using Boke in Python. This technique can be a powerful tool when you need to create visualizations rapidly for whatever purpose you may have.

Needed Initial Libraries & Data Preparation

Below are the initial libraries we need to begin this example and the data preparation.

from pydataset import data
from bokeh.plotting import figure
from bokeh.io import output_file, show
import pandas as pd

df=data("Duncan")

The first line is the code for the data we will use. It loads the data() function from pydataset. Next, we load the figure function from bokeh which will allow us to create our plots. After this we load the output_file() and show() functions which will allow us to display our plots. Lastly, we created our object df which holds our data from the Duncan dataset which has job types, prestige, income, and education as variables.

Multiple Scatterplots

Below is an example of displaying multiple scatterplots. The code and visualization are below followed by an explanation.

# SCATTER PLOT
from bokeh.layouts import column

wc = df.loc[df["type"] == "wc"]
prof = df.loc[df["type"] == "prof"]

fig_one = figure(x_axis_label="Education",y_axis_label="Prestige")
fig_two = figure(x_axis_label="Education",y_axis_label="Prestige")
fig_one.circle(x="education", y="prestige",source=wc,color="blue", legend_label="wc")
fig_two.circle(x="education", y="prestige",source=prof,color="red", legend_label="prof")

output_file(filename="column_plots.html")
show(column(fig_one, fig_two))

You can see the plots are stacked into a single column. The actual setup for this is simple.

ad
  1. We loaded the column() function which allows us to display visualizations in columns
  2. We subsetted the data so that wc workers are in one object and prof are in the other object
  3. We created two figures (fig_one, fig_two) for each of the other datasets. The figures are identical and both will contain education and prestige as the variables
  4. We then added the data to both figures distinguishing the plots by having different colored dots
  5. We created a name for the output
  6. Inside the show() function we used the column() function to display the visualization in columns

All of this code was mostly reviewed, the only new thing was the use of the column() function within the show() function.

Multiple Bar Plots

In this example, we use bar plots and rows instead of columns. The code is followed by the visualization and the explanation.

# bar PLOT
from bokeh.layouts import row

income=pd.DataFrame(df.groupby('type')['income'].mean())
prestige=pd.DataFrame(df.groupby('type')['prestige'].mean())

types = ["prof", "wc", "bc"]
income_type = figure(x_axis_label="type", y_axis_label="income", 
                       x_range=types)
prestige_type = figure(x_axis_label="type", y_axis_label="prestige)", 
                   x_range=types)

# Add bar glyphs
income_type.vbar(x="type", top="income", source=income)
prestige_type.vbar(x="type", top="prestige", source=prestige)

# Generate HTML file and display the subplots
output_file(filename="my_first_column.html")
show(row(income_type, prestige_type))

Here is what we did

  1. We loaded the row() functions which allows us to make rows as you can see.
  2. We calculated the group means for each job type
  3. We created a list called types which included the three job types in our dataset
  4. Next, we made our two figures. One for income and the other for prestige
  5. After this, we added the data to the plots
  6. Lastly, we created the output and showed the visualizations this time using rows

Gridplots

The code below takes a different approach using grid plots. This allows you to set columns and rows for your multiple plots. Below is the code, output, and explanation of this.

from bokeh.layouts import gridplot
from bokeh.models import ColumnDataSource
from bokeh.models import NumeralTickFormatter

plots = []
#df['type'] = df.type.astype('category')
# Complete for loop to create plots
for type in ["bc","wc"]:
    source = ColumnDataSource(data=df)
    df = df.loc[df["type"] == type]
    fig = figure(x_axis_label="education", y_axis_label="income")
    fig.circle(x="education", y="income", source=source, legend_label=type)
    fig.yaxis[0].formatter = NumeralTickFormatter(format="$0a")
    plots.append(fig)

# Display plot
output_file(filename="gridplot.html")
show(gridplot(plots, ncols=2))
  1. We began by loading gridplot(), ColumnSourceData, and NumeralTickFormatter() functions. Gridplot made the grid, columsource created a native data type for bokeh and numeraltickformatter allows us to format the numbers on the axes.
  2. We created an empty list called plots that we will use in our for-loop
  3. We used a for loop to generate the plots. The plots graph education vs income. The NumeralTickFormatter allowed us to display dollar signs on the y-axis
  4. We then displayed the plot

Conclusion

This post provided an example of how to make multiple plots with bokeh. With these tools, there are many different ways they can be utilized for your data purposes.

Leave a ReplyCancel reply