Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How can grid plot in Bokeh library be created with Python?
Bokeh is a powerful Python library for creating interactive data visualizations that render in web browsers. It converts data into JSON format and uses BokehJS (written in TypeScript) to create interactive plots using HTML and JavaScript.
Installation
Install Bokeh using pip or conda ?
pip install bokeh
Or using Anaconda ?
conda install bokeh
Creating a Grid Plot with Image
A grid plot displays data as a 2D image with customizable grid lines. Here's how to create one using mathematical functions ?
import numpy as np
from bokeh.plotting import figure, output_file, show
# Create coordinate arrays
N = 420
x = np.linspace(0, 14, N)
y = np.linspace(0, 14, N)
x_grid, y_grid = np.meshgrid(x, y)
# Generate pattern data using mathematical function
pattern_data = np.sin(x_grid) * np.cos(y_grid)
# Create figure with tooltips
plot = figure(
tooltips=[("x", "$x"), ("y", "$y"), ("value", "@image")],
width=500,
height=500,
title="Grid Plot Example"
)
# Remove padding around plot
plot.x_range.range_padding = 0
plot.y_range.range_padding = 0
# Add image to plot with color palette
plot.image(
image=[pattern_data],
x=0, y=0,
dw=14, dh=14,
palette="Spectral11"
)
# Customize grid appearance
plot.grid.grid_line_width = 0.8
plot.grid.grid_line_color = "white"
# Output to HTML file
output_file("grid_plot.html")
show(plot)
Key Components
| Component | Purpose | Parameters |
|---|---|---|
np.meshgrid() |
Creates coordinate matrices | x, y arrays |
figure() |
Creates plot canvas | tooltips, width, height |
image() |
Renders 2D data as image | image data, position, dimensions, palette |
grid |
Controls grid line appearance | line_width, line_color |
Customizing Grid Appearance
You can modify grid properties to enhance visualization ?
import numpy as np
from bokeh.plotting import figure, output_file, show
# Create sample data
data = np.random.random((50, 50))
plot = figure(width=400, height=400)
plot.image(image=[data], x=0, y=0, dw=10, dh=10, palette="Viridis256")
# Customize grid
plot.grid.grid_line_width = 2
plot.grid.grid_line_color = "red"
plot.grid.grid_line_alpha = 0.5
output_file("custom_grid.html")
show(plot)
Conclusion
Bokeh's grid plots effectively visualize 2D data using the image() function with customizable color palettes. The grid properties can be adjusted to enhance readability and visual appeal.
Advertisements
