不使用bokeh.plotting提供的figure,output_file,show,自己使用bokeh.models中的类实现图像的显示,下面的例子是一个能够显示图像最基本的代码:
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 3 13:25:49 2018
@author: cherish
"""
from bokeh.models import (ColumnDataSource, Plot, DataRange1d,
LinearScale, LinearAxis)
from bokeh.models.glyphs import Rect
from bokeh.resources import INLINE
from bokeh.util.browser import view
from bokeh.document import Document
from bokeh.embed import file_html
# 产生源数据:必须提供
x_y = [(x,y) for x in range(1,5) for y in range(1,5)]
xx,yy = zip(*x_y)
source_data = ColumnDataSource(data = dict(x=xx,y=yy))
# 产生x_range & y_range : 必须提供
xdr, ydr = DataRange1d(), DataRange1d()
# 产生坐标轴类型: 线性类型LinearScale\指数类型LogScale\种类类型CategorialScale : 选填
x_scale, y_scale = LinearScale(), LinearScale()
# 产生Plot实例, 必须填写x_range & y_range参数,其他 参数可以不同写: 必须提供
p=Plot(x_range = xdr, y_range = ydr)
# 产生一个渲染器: 必须提供
rect = Rect(x='x',y='y',width=.5,height=.5)
# 将渲染器加入Plot实例: 必须提供
p.add_glyph(source_data, rect)
# 将坐标轴显示出来: 选填,如果不加入add_layout,坐标轴将不会显示出来
p.add_layout(LinearAxis(), 'below')
p.add_layout(LinearAxis(), 'left')
# 注册Document(): 必须提供
doc = Document()
# 将Plot实例加入Document()中: 必须提供
doc.add_root(p)
if __name__ == "__main__":
doc.validate()
filename = "dateaxis.html"
with open(filename, "w", encoding = 'utf-8') as f:
f.write(file_html(doc, INLINE, "Date Axis Example"))
print("Wrote %s" % filename)
view(filename)
输出的图像为: