数据可视化工具 - bokeh

之前我们介绍过 Python 里面的一些数据分析和可视化工具,比如 Pandas、Modin、Dash 等。今天要介绍一款标星 12.1K 的数据可视化工具 bokeh,优雅、简洁、高性能的交互式可视化库,同时支持大数据量和流式数据。其中 PyPI 和 Conda 每月安装超过 10万+,可见 bokeh 非常受欢迎。

image-20191113204811210

安装方式很简单:pip install bokeh,接下来我们介绍几个示例看一下 bokeh 的使用。

  1. 曲线/折线图

    曲线/折线图日常用的比较多,能够用来直接看一些数据增长趋势,比如数学里面比较典型的几个表示趋势的函数如下:

    通过差不多 20 行代码就能实现上图的效果。

    from bokeh.plotting import figure, output_file, show
       
    # prepare some data
    x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
    y0 = [i**2 for i in x]
    y1 = [10**i for i in x]
    y2 = [10**(i**2) for i in x]
       
    # output to static HTML file
    output_file("log_lines.html")
       
    # create a new plot
    p = figure(
       tools="pan,box_zoom,reset,save",
       y_axis_type="log", y_range=[0.001, 10**11], title="log axis example",
       x_axis_label='sections', y_axis_label='particles'
    )
       
    # add some renderers
    p.line(x, x, legend="y=x")
    p.circle(x, x, legend="y=x", fill_color="white", size=8)
    p.line(x, y0, legend="y=x^2", line_width=3)
    p.line(x, y1, legend="y=10^x", line_color="red")
    p.circle(x, y1, legend="y=10^x", fill_color="red", line_color="red", size=6)
    p.line(x, y2, legend="y=10^x^2", line_color="orange", line_dash="4 4")
       
    # show the results
    show(p)
    
  2. 点状染色图

    这种类型的图可以分析很多的东西,通过色彩、点的大小的不同,但是我每次都觉得非常炫酷,至于能不能直观的看出什么我并不在意,就是那么任性。

import numpy as np

from bokeh.plotting import figure, output_file, show

# prepare some data
N = 4000
x = np.random.random(size=N) * 100
y = np.random.random(size=N) * 100
radii = np.random.random(size=N) * 1.5
colors = [
    "#%02x%02x%02x" % (int(r), int(g), 150) for r, g in zip(50+2*x, 30+2*y)
]

# output to static HTML file (with CDN resources)
output_file("color_scatter.html", title="color_scatter.py example", mode="cdn")

TOOLS = "crosshair,pan,wheel_zoom,box_zoom,reset,box_select,lasso_select"

# create a new plot with the tools above, and explicit ranges
p = figure(tools=TOOLS, x_range=(0, 100), y_range=(0, 100))

# add a circle renderer with vectorized colors and sizes
p.circle(x, y, radius=radii, fill_color=colors, fill_alpha=0.6, line_color=None)

# show the results
show(p)

image-20191113211735071

整体来看,Python 开发的库都比较简洁好用,人生苦短我用 Python 道出了工程师的心声呀。

项目地址:https://github.com/bokeh/bokeh


更多精彩请扫码关注如下公众号。

Written on November 13, 2019