User Avatar

John Loewen

🚀ship30for30

2y ago

Writes about: Python || Mapping || Data Visualization || AI || Storytelling || 💚 Life || Blending these together for insight, and clarity 🎨 🌐 📈 💡

A Beginner's Guide to Image Overlays in Bokeh For Data Scientists To Add Spice to Their Data Visualizations

Here's a simple step-by-step on image overlays with the Python Bokeh Library:

  1. Understanding Bokeh's Glyphs: Bokeh's strength lies in its glyphs—basic visual building blocks.

    For images, the glyphs to focus on are image, image_rgba, and image_url. These allow for flexible image embedding, from watermarks to visuals for data.

  2. Embedding Local Images: To add a local image, convert it to an array using the Pillow library:

from PIL import Image
import numpy as np 
img =  Image.open("path_to_image.png") 
img_array = np.array(img)

Then, use Bokeh's figure.image method to overlay:

from bokeh.plotting import figure, show 
p = figure(width=600, height=400) 
p.image(image=[img_array], x=0, y=0, dw=600, dh=400)
show(p) 
  1. Utilizing URL Images: For web-hosted images, Bokeh's image_url glyph comes handy:

p = figure(x_range=(0,1), y_range=(0,1)) 
p.image_url(url=["https://example.com/image.jpg"], 
x=0.5, y=0.5, w=0.2, h=0.2) 
show(p)
  1. Interactive Image Overlays: Boost engagement by making overlays interactive. Combine images with tools like HoverTool or TapTool. Imagine hovering over a logo to get more company data or tapping on an overlay icon to reveal a data point's details.

The all-in-one writing platform.

Write, publish everywhere, see what works, and become a better writer - all in one place.

Trusted by 80,000+ writers