1 说明:
=====
1.1 Bokeh是专门针对Web浏览器的交互式、可视化Python绘图库 。
1.2 Bokeh,可以做出像D3.js简洁漂亮的交互可视化效果,但是使用难度低于D3.js 。
1.3 不需要使用JAVAscript 。
文章插图
2 官网:
======
https://docs.bokeh.org/en/latest/https://github.com/bokeh/bokeh
3 安装:=====
pip install bokeh#本机安装sudo pip3.8 install bokeh
4 环境:=====
华为笔记本电脑、深度deepin-linux操作系统、python3.8和微软vscode编辑器 。
5 静态基本作图:
============
5.1 柱状图:
5.1.1 代码:
from bokeh.io import output_file, showfrom bokeh.plotting import figure#数据,支持中文fruits = ['苹果', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']counts = [5, 3, 4, 2, 4, 6]#绘图p = figure(x_range=fruits, plot_height=350, title="Fruit Counts",toolbar_location=None, tools="")#柱状图,vbar是指垂直柱状图p.vbar(x=fruits, top=counts, width=0.9)#导出文件:文件名和指定路径,#注意没有这一行,也会自动在代码所在的生成同名的html文件#output_file("/home/xgj/Desktop/bokeh/bar_basic.html")#展示图show(p)
5.1.2 图:文章插图
文章插图
5.2 折线图
5.2.1 代码:
from bokeh.io import output_file, showfrom bokeh.plotting import figure#数据,支持中文fruits = ['苹果', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']counts = [5, 3, 4, 2, 4, 6]#绘图p = figure(x_range=fruits, plot_height=350, title="Fruit Counts",toolbar_location=None, tools="")#柱状图p.line(x=fruits, y=counts)#展示图show(p)
5.2.2 图:文章插图
5.3 散点图:
5.3.1 代码:
#from bokeh.io import output_file, showfrom bokeh.plotting import figure,output_file, show#同上#数据,支持中文fruits = ['苹果', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']counts = [5, 3, 4, 2, 4, 6]#绘图p = figure(x_range=fruits, plot_height=350, title="Fruit Counts",toolbar_location=None, tools="")#柱状图p.scatter(x=fruits, y=counts,size=20, fill_color="#74add1")#展示图show(p)
【Bokeh是一个专门针对Web浏览器的交互式可视化Python库】5.3.2 图:文章插图
===基本作图方便,优美;比matplotlib简单,暂时介绍到这里===
6 高级作图:
=========
6.1 js_events:调用js事件
6.2 代码:
import numpy as npfrom bokeh import eventsfrom bokeh.io import output_file, showfrom bokeh.layouts import column, rowfrom bokeh.models import Button, CustomJS, Divfrom bokeh.plotting import figure#定义js和事件def display_event(div, attributes=[]):style = 'float: left; clear: left; font-size: 13px'return CustomJS(args=dict(div=div), code="""var attrs = %s;var args = [];for (var i = 0; i < attrs.length; i++) {var val = JSON.stringify(cb_obj[attrs[i]], function(key, val) {return val.toFixed ? Number(val.toFixed(2)) : val;})args.push(attrs[i] + '=' + val)}var line = "<span style=%r><b>" + cb_obj.event_name + "</b>(" + args.join(", ") + ")</span>\n";var text = div.text.concat(line);var lines = text.split("\n")if (lines.length > 35)lines.shift();div.text = lines.join("\n");""" % (attributes, style))#数据N = 4000x = np.random.random(size=N) * 100y = np.random.random(size=N) * 100radii = np.random.random(size=N) * 1.5colors = ["#%02x%02x%02x" % (int(r), int(g), 150) for r, g in zip(50+2*x, 30+2*y)]p = figure(tools="pan,wheel_zoom,zoom_in,zoom_out,reset,tap,lasso_select,box_select")#调用散点图p.scatter(x, y, radius=radii,fill_color=colors, fill_alpha=0.6,line_color=None)#容器实例化,宽div = Div(width=1000)button = Button(label="Button", button_type="success", width=300)layout = column(button, row(p, div))#注册事件回调#按钮事件button.js_on_event(events.ButtonClick, display_event(div))# LOD事件p.js_on_event(events.LODStart, display_event(div))p.js_on_event(events.LODEnd, display_event(div))# Point events点事件point_attributes = ['x','y','sx','sy']p.js_on_event(events.Tap,display_event(div, attributes=point_attributes))p.js_on_event(events.DoubleTap, display_event(div, attributes=point_attributes))p.js_on_event(events.Press,display_event(div, attributes=point_attributes))p.js_on_event(events.PressUp,display_event(div, attributes=point_attributes))# Mouse wheel eventp.js_on_event(events.MouseWheel, display_event(div,attributes=point_attributes+['delta']))# Mouse move, enter and leavep.js_on_event(events.MouseMove,display_event(div, attributes=point_attributes))p.js_on_event(events.MouseEnter, display_event(div, attributes=point_attributes))p.js_on_event(events.MouseLeave, display_event(div, attributes=point_attributes))# Pan eventspan_attributes = point_attributes + ['delta_x', 'delta_y']p.js_on_event(events.Pan,display_event(div, attributes=pan_attributes))p.js_on_event(events.PanStart, display_event(div, attributes=point_attributes))p.js_on_event(events.PanEnd,display_event(div, attributes=point_attributes))# Pinch eventspinch_attributes = point_attributes + ['scale']p.js_on_event(events.Pinch,display_event(div, attributes=pinch_attributes))p.js_on_event(events.PinchStart, display_event(div, attributes=point_attributes))p.js_on_event(events.PinchEnd,display_event(div, attributes=point_attributes))# Selection eventsp.js_on_event(events.SelectionGeometry, display_event(div, attributes=['geometry', 'final']))show(layout)
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 为什么你写的代码总是有 Bug?用它来保证 Go 代码质量
- 淘宝轮播图一般是什么内容 淘宝直通车创意轮播和优选哪个好
- 如何提高搜商 淘商机怎么用
- 收下这十点C语言入门须知
- 视频常用的H264和H265是啥?
- 大红袍是绿茶还是红茶,大红袍茶好不好呢
- 蒙汉情深何忍别天涯话斜阳的意思是什么?
- 925银是纯银吗?
- 郭子仪最后的结局是怎样的?
- 淘宝店铺商标注册号申请号是什么 怎么申请靓号