Interactive plots and applications in the browser from Python
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
The high-level plotting interface centered around the figure() function, providing the primary API for creating interactive visualizations in Bokeh. This interface includes 70+ glyph methods for different chart types, built-in interaction tools, and automatic styling and legend management.
The main entry point for creating plots with customizable dimensions, tools, styling, and axis configuration.
def figure(x_range=None, y_range=None, x_axis_type='auto', y_axis_type='auto',
x_axis_location='below', y_axis_location='left', x_axis_label=None, y_axis_label=None,
width=600, height=600, title=None, title_location='above',
tools=None, toolbar_location='above', toolbar_sticky=True,
left=None, right=None, above=None, below=None,
min_border=5, min_border_left=None, min_border_right=None,
min_border_top=None, min_border_bottom=None,
lod_factor=10, lod_threshold=2000, lod_interval=300, lod_timeout=500,
hidpi=True, output_backend='canvas', match_aspect=False, aspect_scale=1,
sizing_mode='fixed', **kwargs):
"""
Create a new Figure for plotting.
Parameters:
- x_range, y_range: Range objects or tuples for axis ranges
- x_axis_type, y_axis_type: 'auto', 'linear', 'log', 'datetime', 'mercator'
- width, height: Plot dimensions in pixels
- title: Plot title string
- tools: String or list of tool names/objects
- toolbar_location: 'above', 'below', 'left', 'right', None
- sizing_mode: 'fixed', 'stretch_width', 'stretch_height', 'stretch_both', 'scale_width', 'scale_height', 'scale_both'
Returns:
Figure object with glyph methods
"""
def markers():
"""Print available marker types."""
DEFAULT_TOOLS: str # "pan,wheel_zoom,box_zoom,save,reset,help"Methods for creating line-based visualizations including simple lines, multi-line plots, and step plots.
def line(self, x='x', y='y', line_width=1, line_color='black', line_alpha=1.0,
line_join='bevel', line_cap='butt', line_dash=[], line_dash_offset=0,
**kwargs):
"""
Plot lines connecting data points.
Parameters:
- x, y: Field names or arrays for coordinates
- line_width: Line thickness in pixels
- line_color: Line color
- line_alpha: Line transparency (0-1)
- line_join: 'miter', 'round', 'bevel'
- line_cap: 'butt', 'round', 'square'
- line_dash: Dash pattern as list of integers
"""
def multi_line(self, xs='xs', ys='ys', line_width=1, line_color='black',
line_alpha=1.0, line_join='bevel', line_cap='butt',
line_dash=[], line_dash_offset=0, **kwargs):
"""
Plot multiple disconnected lines.
Parameters:
- xs, ys: Field names or lists of arrays for multiple line coordinates
"""
def step(self, x='x', y='y', mode='before', line_width=1, line_color='black',
line_alpha=1.0, **kwargs):
"""
Plot step lines.
Parameters:
- mode: 'before', 'after', 'center' - step positioning
"""
def segment(self, x0='x0', y0='y0', x1='x1', y1='y1', **kwargs):
"""Plot line segments between point pairs."""
def ray(self, x='x', y='y', length='length', angle='angle', **kwargs):
"""Plot rays from points with specified length and angle."""
def bezier(self, x0='x0', y0='y0', x1='x1', y1='y1', cx0='cx0', cy0='cy0',
cx1='cx1', cy1='cy1', **kwargs):
"""Plot Bezier curves."""
def quadratic(self, x0='x0', y0='y0', x1='x1', y1='y1', cx='cx', cy='cy', **kwargs):
"""Plot quadratic curves."""Methods for creating scatter plots and marker-based visualizations with 25+ marker types.
def scatter(self, x='x', y='y', size='size', marker='circle', color='color',
alpha=1.0, **kwargs):
"""
General scatter plot with configurable marker types.
Parameters:
- x, y: Field names or arrays for coordinates
- size: Marker size in screen units
- marker: Marker type ('circle', 'square', 'triangle', etc.)
- color: Marker fill color
- alpha: Marker transparency (0-1)
"""
def circle(self, x='x', y='y', size=4, radius=None, radius_dimension='x',
fill_color='gray', fill_alpha=1.0, line_color='black',
line_width=1, line_alpha=1.0, **kwargs):
"""
Circle markers.
Parameters:
- size: Marker diameter in screen units (if radius not specified)
- radius: Circle radius in data units
- radius_dimension: 'x', 'y', 'max', 'min' - scaling dimension for radius
"""
# Specific marker methods
def asterisk(self, x='x', y='y', size=4, **kwargs): ...
def circle_cross(self, x='x', y='y', size=4, **kwargs): ...
def circle_dot(self, x='x', y='y', size=4, **kwargs): ...
def circle_x(self, x='x', y='y', size=4, **kwargs): ...
def circle_y(self, x='x', y='y', size=4, **kwargs): ...
def cross(self, x='x', y='y', size=4, **kwargs): ...
def dash(self, x='x', y='y', size=4, **kwargs): ...
def diamond(self, x='x', y='y', size=4, **kwargs): ...
def diamond_cross(self, x='x', y='y', size=4, **kwargs): ...
def diamond_dot(self, x='x', y='y', size=4, **kwargs): ...
def dot(self, x='x', y='y', size=4, **kwargs): ...
def hex(self, x='x', y='y', size=4, **kwargs): ...
def hex_dot(self, x='x', y='y', size=4, **kwargs): ...
def inverted_triangle(self, x='x', y='y', size=4, **kwargs): ...
def plus(self, x='x', y='y', size=4, **kwargs): ...
def square(self, x='x', y='y', size=4, **kwargs): ...
def square_cross(self, x='x', y='y', size=4, **kwargs): ...
def square_dot(self, x='x', y='y', size=4, **kwargs): ...
def square_pin(self, x='x', y='y', size=4, **kwargs): ...
def square_x(self, x='x', y='y', size=4, **kwargs): ...
def star(self, x='x', y='y', size=4, **kwargs): ...
def star_dot(self, x='x', y='y', size=4, **kwargs): ...
def triangle(self, x='x', y='y', size=4, **kwargs): ...
def triangle_dot(self, x='x', y='y', size=4, **kwargs): ...
def triangle_pin(self, x='x', y='y', size=4, **kwargs): ...
def x(self, x='x', y='y', size=4, **kwargs): ...
def y(self, x='x', y='y', size=4, **kwargs): ...Methods for creating geometric shapes, bars, and filled areas.
def rect(self, x='x', y='y', width='width', height='height', angle=0.0,
dilate=False, **kwargs):
"""
Rectangular glyphs.
Parameters:
- x, y: Center coordinates
- width, height: Dimensions in data units
- angle: Rotation angle in radians
- dilate: Whether to dilate pixel boundaries
"""
def quad(self, left='left', right='right', top='top', bottom='bottom', **kwargs):
"""
Quadrilateral glyphs using explicit boundaries.
Parameters:
- left, right, top, bottom: Boundary coordinates
"""
def ellipse(self, x='x', y='y', width='width', height='height', angle=0.0, **kwargs):
"""Elliptical shapes."""
def wedge(self, x='x', y='y', radius='radius', start_angle='start_angle',
end_angle='end_angle', direction='anticlock', **kwargs):
"""
Wedge/pie slice shapes.
Parameters:
- direction: 'clock', 'anticlock'
"""
def annular_wedge(self, x='x', y='y', inner_radius='inner_radius',
outer_radius='outer_radius', start_angle='start_angle',
end_angle='end_angle', direction='anticlock', **kwargs):
"""Annular wedge shapes (ring segments)."""
def annulus(self, x='x', y='y', inner_radius='inner_radius',
outer_radius='outer_radius', **kwargs):
"""Ring/annulus shapes."""
def arc(self, x='x', y='y', radius='radius', start_angle='start_angle',
end_angle='end_angle', direction='anticlock', **kwargs):
"""Arc shapes."""Methods for creating vertical and horizontal bar charts.
def vbar(self, x='x', top='top', width=0.8, bottom=0, **kwargs):
"""
Vertical bars.
Parameters:
- x: Bar center x-coordinates
- top: Bar top y-coordinates
- width: Bar width in data units
- bottom: Bar bottom y-coordinates (default: 0)
"""
def hbar(self, y='y', right='right', height=0.8, left=0, **kwargs):
"""
Horizontal bars.
Parameters:
- y: Bar center y-coordinates
- right: Bar right x-coordinates
- height: Bar height in data units
- left: Bar left x-coordinates (default: 0)
"""Methods for creating filled area plots and spans.
def varea(self, x='x', y1='y1', y2='y2', **kwargs):
"""
Vertical area between two y-coordinates.
Parameters:
- x: X-coordinates
- y1, y2: Bottom and top y-coordinates
"""
def harea(self, y='y', x1='x1', x2='x2', **kwargs):
"""Horizontal area between two x-coordinates."""
def varea_step(self, x='x', y1='y1', y2='y2', step_mode='before', **kwargs):
"""Step-wise vertical area."""
def harea_step(self, y='y', x1='x1', x2='x2', step_mode='before', **kwargs):
"""Step-wise horizontal area."""
def vspan(self, x='x', line_width=1, line_color='black', **kwargs):
"""Vertical spans across entire plot height."""
def hspan(self, y='y', line_width=1, line_color='black', **kwargs):
"""Horizontal spans across entire plot width."""
def vstrip(self, x0='x0', x1='x1', **kwargs):
"""Vertical strips between x-coordinates."""
def hstrip(self, y0='y0', y1='y1', **kwargs):
"""Horizontal strips between y-coordinates."""Methods for displaying images and complex polygonal shapes.
def image(self, image='image', x='x', y='y', dw='dw', dh='dh',
palette=None, dilate=False, **kwargs):
"""
Display 2D image data.
Parameters:
- image: 2D array of image data
- x, y: Lower-left corner coordinates
- dw, dh: Image dimensions in data units
- palette: Color palette for scalar data
"""
def image_rgba(self, image='image', x='x', y='y', dw='dw', dh='dh',
dilate=False, **kwargs):
"""Display RGBA image data (uint32 array)."""
def image_stack(self, image='image', x='x', y='y', dw='dw', dh='dh',
palette=None, **kwargs):
"""Display stacked images."""
def image_url(self, url='url', x='x', y='y', w='w', h='h', angle=0.0,
dilate=False, anchor='top_left', retry_attempts=0,
retry_timeout=0, **kwargs):
"""
Display images from URLs.
Parameters:
- url: Image URL
- w, h: Image dimensions in screen units
- anchor: 'top_left', 'top_center', 'top_right', etc.
"""
def patch(self, x='x', y='y', **kwargs):
"""Single polygon patch."""
def patches(self, xs='xs', ys='ys', **kwargs):
"""Multiple polygon patches."""
def multi_polygons(self, xs='xs', ys='ys', **kwargs):
"""Complex polygonal shapes with holes."""
def hex_tile(self, q='q', r='r', size=1.0, aspect_scale=1.0,
scale=1.0, orientation='pointytop', **kwargs):
"""
Hexagonal tiles for hex mapping.
Parameters:
- q, r: Hex grid coordinates
- orientation: 'pointytop', 'flattop'
"""Methods for adding text annotations and mathematical expressions.
def text(self, x='x', y='y', text='text', angle=0.0, x_offset=0, y_offset=0,
text_font='helvetica', text_font_size='16px', text_font_style='normal',
text_color='black', text_alpha=1.0, text_align='left',
text_baseline='bottom', **kwargs):
"""
Text annotations.
Parameters:
- text: Text strings
- angle: Rotation angle in radians
- x_offset, y_offset: Offset in screen units
- text_align: 'left', 'right', 'center'
- text_baseline: 'top', 'middle', 'bottom', 'alphabetic', 'hanging'
"""
def mathml(self, x='x', y='y', text='text', **kwargs):
"""MathML mathematical notation."""
def tex(self, x='x', y='y', text='text', **kwargs):
"""TeX/LaTeX mathematical notation."""Additional specialized glyph methods.
def ngon(self, x='x', y='y', size='size', n=5, **kwargs):
"""
N-sided regular polygons.
Parameters:
- n: Number of sides
"""
def block(self, x='x', y='y', width='width', height='height', **kwargs):
"""Block markers for heatmaps and categorical data."""Common functions re-exported from other modules for convenience.
# From bokeh.io
def show(obj, browser=None, new=None):
"""Display plot in browser or notebook."""
def save(obj, filename=None, resources=None, title=None):
"""Save plot to HTML file."""
def output_file(filename, title=None, mode=None, root_dir=None):
"""Direct output to HTML file."""
def output_notebook(hide_banner=None, load_timeout=5000, notebook_type='jupyter'):
"""Direct output to Jupyter notebook."""
def reset_output():
"""Reset output mode."""
def curdoc():
"""Get current document."""
# From bokeh.layouts
def column(*children, **kwargs):
"""Arrange plots/widgets vertically."""
def row(*children, **kwargs):
"""Arrange plots/widgets horizontally."""
def gridplot(children, **kwargs):
"""Create grid of plots with shared tools."""
# From bokeh.models
class ColumnDataSource:
"""Primary data source."""
def __init__(self, data=None, **kwargs): ...
class Document:
"""Document container."""
# Layout containers
class Row: ...
class Column: ...from bokeh.plotting import figure, show
import numpy as np
x = np.linspace(0, 4*np.pi, 100)
p = figure(width=400, height=400, title="Multiple Lines")
# Multiple line series with different styles
p.line(x, np.sin(x), legend_label="sin(x)", line_width=2, color='blue')
p.line(x, np.cos(x), legend_label="cos(x)", line_width=2, color='red', line_dash='dashed')
p.legend.location = "top_right"
show(p)from bokeh.plotting import figure, show
from bokeh.models import HoverTool
# Sample data
n = 500
x = np.random.random(n) * 100
y = np.random.random(n) * 100
colors = np.random.choice(['red', 'green', 'blue', 'orange', 'purple'], n)
sizes = np.random.randint(10, 30, n)
# Create figure with hover tool
p = figure(width=600, height=600, title="Customized Scatter Plot",
tools="pan,wheel_zoom,box_zoom,reset,save")
# Add hover tool
hover = HoverTool(tooltips=[("Index", "$index"), ("(X,Y)", "($x, $y)"), ("Size", "@size")])
p.add_tools(hover)
# Create scatter plot
p.circle(x, y, size=sizes, color=colors, alpha=0.6)
show(p)from bokeh.plotting import figure, show
categories = ['Q1', 'Q2', 'Q3', 'Q4']
values = [20, 25, 30, 35]
p = figure(x_range=categories, width=400, height=400, title="Quarterly Sales")
p.vbar(x=categories, top=values, width=0.5, color='navy', alpha=0.8)
p.xgrid.grid_line_color = None
p.y_range.start = 0
show(p)