[python]bokeh.models实现的一个例子

不使用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)
输出的图像为:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值