Last modified: Jan 01, 2025 By Alexander Williams

Python Bokeh Scatter Plot Tutorial with Examples

Scatter plots are essential tools for visualizing relationships between variables in data analysis. Bokeh's scatter() method provides a powerful way to create interactive scatter plots with customizable features.

Getting Started with Bokeh Scatter Plots

Before creating scatter plots, ensure you have Bokeh installed. If you haven't installed it yet, check out our guide on how to install and get started with Python Bokeh.

Basic Scatter Plot Example

Let's start with a simple scatter plot using random data points:


from bokeh.plotting import figure, show
import numpy as np

# Generate random data
x = np.random.rand(50)
y = np.random.rand(50)

# Create figure
p = figure(title='Basic Scatter Plot')

# Create scatter plot
p.scatter(x, y, size=10)

# Show plot
show(p)

Customizing Marker Appearance

Bokeh offers various marker types and customization options to enhance your scatter plots. Here's an example showing different marker types:


from bokeh.plotting import figure, show
import numpy as np

# Create sample data
x = np.linspace(0, 10, 50)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)

# Create figure
p = figure(title='Scatter Plot with Different Markers')

# Add different marker types
p.scatter(x, y1, marker='circle', size=10, color='blue', legend_label='Circle')
p.scatter(x, y2, marker='square', size=10, color='red', legend_label='Square')
p.scatter(x, y3, marker='triangle', size=10, color='green', legend_label='Triangle')

# Show plot
show(p)

Adding Interactive Features

One of Bokeh's strengths is its interactive features. Let's create a scatter plot with hover tooltips:


from bokeh.plotting import figure, show
from bokeh.models import HoverTool
import numpy as np

# Generate data
x = np.random.rand(100)
y = np.random.rand(100)
sizes = np.random.randint(10, 50, 100)

# Create figure with hover tool
hover = HoverTool(tooltips=[
    ('x', '@x{0.000}'),
    ('y', '@y{0.000}'),
    ('size', '@size')
])

p = figure(title='Interactive Scatter Plot', tools=[hover, 'pan', 'wheel_zoom', 'reset'])

# Create scatter plot with size variation
p.scatter(x, y, size=sizes, fill_alpha=0.6, line_color='navy', fill_color='blue')

show(p)

Color Mapping Based on Data

You can map colors to data values using Bokeh's color mappers:


from bokeh.plotting import figure, show
from bokeh.transform import linear_cmap
import numpy as np

# Generate data
N = 100
x = np.random.normal(size=N)
y = np.random.normal(size=N)
color_data = x * y

# Create color mapper
mapper = linear_cmap(field_name='color', palette='Viridis256', low=min(color_data), high=max(color_data))

# Create figure
p = figure(title='Scatter Plot with Color Mapping')

# Create scatter plot with color mapping
p.scatter(x, y, color=mapper, source=dict(color=color_data))

show(p)

Combining with Other Plot Types

Scatter plots can be combined with other plot types like lines. For more information about line plots, check out our Python Bokeh Line Plot Tutorial.

Saving and Displaying Plots

To save your scatter plots as HTML files, you can use Bokeh's output_file functionality.

Best Practices and Tips

Size Considerations: Choose appropriate marker sizes based on your data density and plot dimensions.

Color Selection: Use contrasting colors for better visibility and consider color-blind friendly palettes.

Interactivity: Add relevant hover tooltips to make your plots more informative and user-friendly.

Conclusion

Bokeh's scatter() method provides a versatile way to create interactive and visually appealing scatter plots. With its extensive customization options, you can create effective visualizations for your data analysis needs.