Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 287
Trevor McCormick Avatar asked Jan 07 '16 02:01

Trevor McCormick


People also ask

Does Bokeh use Matplotlib?

Matplotlib, seaborn, ggplot, and Pandas¶Uses bokeh to display a Matplotlib Figure. You can store a bokeh plot in a standalone HTML file, as a document in a Bokeh plot server, or embedded directly into an IPython Notebook output cell. Parameters: fig (matplotlib.

How to work with images in bokeh(Python)?

How to work with images in Bokeh (Python)? To work with images in Bokeh, use image_url () method and pass a list of images. Configure the default output state to generate output saved to a file when :func:'show' is called. Create a new Figure for plotting. Render the images loaded from the given URLs.

What is bokeh in photography?

After.png 1. Before.png Bokeh is the Japanese word which means Blur. Bokeh tends to the region which we choose to out of focus. This effect makes the in-focus image so vibrant and clear to eyes which makes the photo looks more elegant.

How do you make a bokeh plot in Python?

There are easier ways to create a basic histogram in Python, and the same result could be done using a few lines of matplotlib. However, the payoff in the development required for a Bokeh plot comes in the tools and ways to interact with the data that we can now easily add to the graph.

What data types does bokeh support?

The examples above used Python lists and Numpy arrays to represent the data, and Bokeh is well equipped to handle these datatypes. However, when it comes to data in Python, you are most likely going to come across Python dictionaries and Pandas DataFrames, especially if you’re reading in data from a file or external data source.


2 Answers

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

like image 138
Luke Canavan Avatar answered Oct 06 '22 01:10

Luke Canavan


The earlier answer was helpful. However, I wanted an image only option without any additional object. So, adding the answer for Bokeh version 0.12.0 and removed all the grids, axes and toolbar.

from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource, Range1d

bosch_logo = "static/tree.jpg"
logo_src = ColumnDataSource(dict(url = [bosch_logo]))

page_logo = figure(plot_width = 500, plot_height = 500, title="")
page_logo.toolbar.logo = None
page_logo.toolbar_location = None
page_logo.x_range=Range1d(start=0, end=1)
page_logo.y_range=Range1d(start=0, end=1)
page_logo.xaxis.visible = None
page_logo.yaxis.visible = None
page_logo.xgrid.grid_line_color = None
page_logo.ygrid.grid_line_color = None
page_logo.image_url(url='url', x=0.05, y = 0.85, h=0.7, w=0.9, source=logo_src)
page_logo.outline_line_alpha = 0 
curdoc().add_root(page_logo)
like image 26
Jayant Avatar answered Oct 06 '22 02:10

Jayant