Last modified: Jan 02, 2025 By Alexander Williams
Python Bokeh gridplot: Create Multi-Plot Grid Layouts
When working with multiple data visualizations, organizing plots in a grid layout can enhance readability and presentation. Bokeh's gridplot()
function provides an elegant solution for arranging multiple plots in a structured grid format.
Understanding gridplot Basics
The gridplot
function from Bokeh allows you to combine multiple plot objects into a single organized grid layout. This is particularly useful when comparing different datasets or showing related visualizations side by side.
Let's start with a simple example that creates a 2x2 grid of plots. First, we'll need to import the necessary modules and create some sample data.
from bokeh.layouts import gridplot
from bokeh.plotting import figure, show
import numpy as np
# Create sample data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = x**2
y4 = np.exp(x/10)
# Create four different plots
p1 = figure(title="Sine Wave")
p1.line(x, y1, line_color="blue")
p2 = figure(title="Cosine Wave")
p2.line(x, y2, line_color="red")
p3 = figure(title="Quadratic")
p3.line(x, y3, line_color="green")
p4 = figure(title="Exponential")
p4.line(x, y4, line_color="purple")
# Arrange plots in a grid
grid = gridplot([[p1, p2], [p3, p4]])
show(grid)
Customizing Grid Layouts
You can create different grid arrangements by modifying the nested list structure. The grid layout follows a row-major order, where each inner list represents a row in the grid.
Here's how to create a 3x1 vertical layout:
# Create three plots in a vertical layout
vertical_grid = gridplot([[p1], [p2], [p3]])
show(vertical_grid)
Setting Plot Dimensions
You can control the size of individual plots using the width and height parameters when creating figures. This helps maintain consistency across your grid layout.
# Create plots with specific dimensions
p1 = figure(title="Plot 1", width=300, height=300)
p2 = figure(title="Plot 2", width=300, height=300)
p3 = figure(title="Plot 3", width=300, height=300)
p4 = figure(title="Plot 4", width=300, height=300)
# Create different types of plots
p1.circle(x, y1, size=5, color="navy") # Scatter plot
p2.line(x, y2, line_width=2, color="red") # Line plot
p3.vbar(x=x[::10], top=y3[::10], width=0.5) # Bar plot
p4.patch(x, y4, alpha=0.6, color="green") # Area plot
grid = gridplot([[p1, p2], [p3, p4]], sizing_mode="stretch_both")
show(grid)
Advanced Grid Features
The gridplot
function offers several advanced features for customizing your layout. You can specify toolbars location and sizing modes to create responsive layouts.
# Create a responsive grid with shared toolbars
grid = gridplot(
[[p1, p2], [p3, p4]],
toolbar_location="right", # Place toolbar on the right
sizing_mode="stretch_width", # Make grid responsive
toolbar_options={"logo": None} # Remove Bokeh logo
)
show(grid)
Combining Different Plot Types
One of the powerful features of gridplot
is the ability to combine different types of visualizations. Let's create a dashboard-like layout combining various plot types.
For more information about creating different types of plots, check out our guides on scatter plots and line plots.
Best Practices and Tips
When working with gridplot
, keep these important considerations in mind:
- Maintain consistent plot sizes for a professional appearance
- Use appropriate spacing between plots
- Consider the overall layout proportions
- Ensure clear titles and labels for each plot
Conclusion
Bokeh's gridplot
function is a versatile tool for creating organized multi-plot layouts. By understanding its features and capabilities, you can create professional-looking dashboards and visualization arrangements.
For more advanced plot customization, you might want to explore adding custom layouts and annotations to your plots.