Cover Image for How to Plot Multiple Plots using Bokeh in Python
141 views

How to Plot Multiple Plots using Bokeh in Python

The Bokeh can create multiple plots within the same figure by adding multiple glyphs (e.g., lines, circles, rectangles) or different kinds of plots (e.g., scatter plots, bar plots) to the same figure. Each plot can be customized individually, and you can arrange them in a grid or stack them vertically or horizontally as needed.

Here’s an example of how to plot multiple plots using Bokeh in Python:

from bokeh.plotting import figure, show, output_file
from bokeh.layouts import gridplot

# Sample data
x = [1, 2, 3, 4, 5]
y1 = [10, 8, 6, 4, 2]
y2 = [2, 4, 6, 8, 10]

# Create Bokeh figures for each plot
p1 = figure(title="Plot 1", x_axis_label="X-axis", y_axis_label="Y-axis")
p1.line(x, y1, line_width=2, legend_label="Line 1", color="blue")

p2 = figure(title="Plot 2", x_axis_label="X-axis", y_axis_label="Y-axis")
p2.circle(x, y2, size=8, legend_label="Circle 1", color="red")

# Arrange the plots in a grid
grid = gridplot([[p1, p2]])

# Output to an HTML file
output_file("multiple_plots.html")

# Show the plots
show(grid)

In this example:

  1. We import the necessary modules from Bokeh, including figure, show, output_file, and gridplot.
  2. We define sample data for two different plots (y1 and y2).
  3. We create Bokeh figures (p1 and p2) for each plot. We customize each figure by specifying titles, axis labels, line widths, marker sizes, legend labels, and colors.
  4. We arrange the plots in a grid using the gridplot function. You can adjust the layout of the grid as needed to arrange the plots vertically, horizontally, or in a more complex grid pattern.
  5. We specify an output file where the plots will be saved as an HTML file using output_file.
  6. Finally, we display the grid of plots using show().

When you run this code, it will generate an HTML file named “multiple_plots.html” containing both plots arranged in a grid. You can customize this example by adding more plots, changing their appearance, or adjusting the grid layout to create more complex multi-plot visualizations.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS