Search code examples
pythonimagematplotlibbokeh

How do I work with images in Bokeh (Python)


For example you can plot an image in matplotlib using this code:

%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img=mpimg.imread('image.png')
plt.imshow(img)

Is something like this possible with Bokeh(0.10)?


Solution

  • You can use the ImageURL glyph (image_url plot method)to load images locally or from the web.

    from bokeh.plotting import figure, show, output_file
    
    output_file('image.html')
    
    p = figure(x_range=(0,1), y_range=(0,1))
    p.image_url(url=['tree.png'], x=0, y=1, w=0.8, h=0.6)
    ## could also leave out keywords
    # p.image_url(['tree.png'], 0, 1, 0.8, h=0.6)  
    show(p)
    

    Image Example

    One gotcha - if you graph only an image (and no other data), you'll have to explicitly set the plot ranges.

    Here's the docs:

    http://docs.bokeh.org/en/latest/docs/reference/models/glyphs.html#bokeh.models.glyphs.ImageURL