Python 绘图 - Bokeh 初探

2020-02-02  本文已影响0人  不忘初心2017

背景

想了解一下 python 中的几个绘图工具,及其应用场景。bokeh 为其一.

先放一段 Bokeh 的 Vision:

Bokeh is an interactive visualization library that targets modern web browsers for presentation. Its goal is to provide elegant, concise construction of versatile graphics, and to extend this capability with high-performance interactivity over very large or streaming datasets. Bokeh can help anyone who would like to quickly and easily create interactive plots, dashboards, and data applications.

焦点词:交互式,高性能,浏览器。

安装

pip install bokeh 或使用镜像 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple bokeh

跑个 demo

参考:Quickstart — Bokeh 1.4.0 documentation

基本步骤:

demo 代码:

from bokeh.plotting import figure, output_file, show

# prepare some data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

# output to static HTML file
output_file("lines.html")

# create a new plot with a title and axis labels
p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')

# add a line renderer with legend and line thickness
p.line(x, y, legend_label="Temp.", line_width=2)

# show the results
show(p)

实际图形就不挂出来了,跑一下代码立刻生成。

输出到 Jupyter Notebook

如果是在 Jupyter Notebook 中,用 output_notebook():

from bokeh.io import output_notebook
output_notebook()

理解基本概念

参考:Defining Key Concepts — Bokeh 1.4.0 documentation

Bokeh is an interactive visualization library for modern web browsers. It provides elegant, concise construction of versatile graphics, and affords high-performance interactivity over large or streaming datasets. Bokeh can help anyone who would like to quickly and easily make interactive plots, dashboards, and data applications.

在浏览器中 交互式图表库,interactive 怎么理解呢?

一个简单的例子,比如Interactive Legends — Bokeh 1.4.0 documentation,图表中的图例,可以点击某个或某几个显示或隐藏。

再比如,Adding Widgets — Bokeh 1.4.0 documentation,在浏览器页面上添加 button 等控件。

Bokeh Python 库 和 Bokeh 客户端库 BokehJS(有自己的API), 负责浏览器上图形的绘制和 render,所以这些交互式操作有 JS 的支持,这估计是 bokeh 的一大特色。

大致的结构和接口:

- 最上层:**BokehJS**          render UI and handle UI interactions
            |--- Documents
                    |---Models:
                          |------plots
                                  |------glyphs
                          |------widgets 
                                
                    |---Data:      图表所需的数据
                    |---Widgets:   交互式控件,如 button 等
                    

- 中间层: **boken.plotting**
            |---figure()  -> Figure Model

- 最底层: **bokeh.models**

References

上一篇 下一篇

猜你喜欢

热点阅读