How to work with images in Bokeh (Python)?

To work with images in Bokeh, you can use the image_url() method to display images from URLs or local files. This method renders images as plot elements that can be positioned and sized within your visualization.

Basic Image Display

The following example demonstrates how to display an image using Bokeh ?

from bokeh.plotting import figure, show, output_file
from bokeh.io import curdoc

# Configure output to HTML file
output_file('image_example.html')

# Create a figure with specified ranges
p = figure(x_range=(0, 1), y_range=(0, 1), 
           width=600, height=400,
           title="Image Display Example")

# Add an image using a placeholder URL
p.image_url(url=["https://via.placeholder.com/400x300/blue/white?text=Sample+Image"], 
           x=0.1, y=0.9, w=0.8, h=0.6, anchor="top_left")

show(p)

Image Parameters

The image_url() method accepts several important parameters ?

Parameter Description Example
url List of image URLs ["image1.jpg", "image2.png"]
x, y Position coordinates x=0, y=1
w, h Width and height w=0.8, h=0.6
anchor Anchor point "top_left", "center"

Multiple Images Example

You can display multiple images by passing lists of URLs and coordinates ?

from bokeh.plotting import figure, show, output_file

output_file('multiple_images.html')

p = figure(x_range=(0, 2), y_range=(0, 2), 
           width=700, height=500,
           title="Multiple Images")

# Display multiple placeholder images
urls = [
    "https://via.placeholder.com/200x150/red/white?text=Image+1",
    "https://via.placeholder.com/200x150/green/white?text=Image+2",
    "https://via.placeholder.com/200x150/blue/white?text=Image+3"
]

x_positions = [0.1, 0.6, 1.1]
y_positions = [1.5, 1.0, 0.5]

p.image_url(url=urls, 
           x=x_positions, 
           y=y_positions, 
           w=0.4, h=0.3,
           anchor="bottom_left")

show(p)

Working with Local Images

For local images, you need to provide the correct file path ?

from bokeh.plotting import figure, show, output_file
import os

output_file('local_image.html')

# Ensure the image file exists in your working directory
image_path = "path/to/your/image.jpg"

p = figure(x_range=(0, 1), y_range=(0, 1), 
           width=600, height=400)

if os.path.exists(image_path):
    p.image_url(url=[image_path], x=0, y=1, w=0.8, h=0.6)
else:
    print(f"Image not found: {image_path}")

show(p)

Key Points

  • Use output_file() to specify where to save the HTML output
  • Images are positioned using plot coordinates, not pixel coordinates
  • The anchor parameter determines which part of the image aligns with the x,y coordinates
  • Both web URLs and local file paths are supported
  • Multiple images can be displayed by passing lists of parameters

Conclusion

Bokeh's image_url() method provides a flexible way to incorporate images into your visualizations. You can display single or multiple images, position them precisely, and control their size and anchor points for effective data presentation.

Updated on: 2026-03-25T21:32:40+05:30

819 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements