How can Bokeh be used to generate scatter plot using Python?

Bokeh is a Python package that helps in data visualization. It is an open source project that renders plots using HTML and JavaScript, making it useful for web-based dashboards. Bokeh converts the data source into a JSON file which is used as input to BokehJS, a JavaScript library written in TypeScript that renders visualizations on modern browsers.

Unlike Matplotlib and Seaborn which produce static plots, Bokeh produces interactive plots that change when users interact with them. Plots can be embedded in Flask or Django web applications and rendered in Jupyter notebooks.

Installation

Install Bokeh using pip or conda ?

pip3 install bokeh

Or using Anaconda ?

conda install bokeh

Creating a Basic Scatter Plot

The scatter() method creates scatter plots with customizable markers, colors, and sizes ?

from bokeh.plotting import figure, output_file, show

# Create a figure with specified dimensions
fig = figure(plot_width=500, plot_height=400, title="Sample Scatter Plot")

# Add scatter points with customization
fig.scatter([1, 3, 7, 5, 4, 9], [6, 5, 9, 8, 0, 1], 
           marker="circle", size=20, fill_color="grey", line_color="black")

# Specify output file and display
output_file('scatterplot.html')
show(fig)

This creates an interactive HTML file named 'scatterplot.html' with the scatter plot.

Customizing Scatter Plot Appearance

You can customize markers, colors, and add labels ?

from bokeh.plotting import figure, output_file, show

# Sample data
x_values = [1, 2, 3, 4, 5, 6]
y_values = [6, 7, 2, 4, 5, 8]

# Create figure with title and axis labels
fig = figure(plot_width=600, plot_height=400, 
            title="Customized Scatter Plot",
            x_axis_label='X-axis', 
            y_axis_label='Y-axis')

# Create scatter plot with multiple customizations
fig.scatter(x_values, y_values, 
           marker="diamond", 
           size=15, 
           fill_color="blue", 
           fill_alpha=0.6,
           line_color="navy", 
           line_width=2)

output_file('custom_scatter.html')
show(fig)

Key Parameters

Parameter Description Example Values
marker Shape of scatter points "circle", "square", "diamond"
size Size of markers in pixels 10, 20, 30
fill_color Interior color of markers "red", "blue", "#FF5733"
line_color Border color of markers "black", "white", "navy"

Dependencies

Bokeh requires the following Python packages ?

NumPy
Pillow
Jinja2
Packaging
PyYAML
Six
Tornado
Python-dateutil

Conclusion

Bokeh's scatter() method creates interactive scatter plots that can be customized with various markers, colors, and sizes. The generated HTML files can be easily embedded in web applications or viewed in browsers.

Updated on: 2026-03-25T15:01:31+05:30

329 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements