可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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)? Thanks in advance for your help.
回答1:
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) show(p)

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://bokeh.pydata.org/en/latest/docs/reference/models/glyphs.html#bokeh.models.glyphs.ImageURL
回答2:
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)
回答3:
Running this example using bokeh serve is a bit more tricky. I suggest to setup working directory properly:
server_folder/ +main.py +static/ +logo.png
.. and run bokeh serve command from directory ABOVE server_folder
bokeh serve server_folder --show
Then this code works for me
#main.py file from bokeh.plotting import figure, curdoc x_range = (-20,-10) # could be anything - e.g.(0,1) y_range = (20,30) p = figure(x_range=x_range, y_range=y_range) #img_path = 'https://bokeh.pydata.org/en/latest/_static/images/logo.png' img_path = 'server_folder/static/logo.png' p.image_url(url=[img_path],x=x_range[0],y=y_range[1],w=x_range[1]-x_range[0],h=y_range[1]-y_range[0]) doc = curdoc() doc.add_root(p)
