Python Bokeh数据可视化教程

时间:2020-02-23 14:42:11  来源:igfitidea点击:

Bokeh是一个交互式Python数据可视化库,以现代Web浏览器为对象进行演示。

Python Bokeh库旨在以快速,简便的方式和优雅的方式,在非常大的甚至是流数据集上,以新颖的图形的简洁结构提供高性能的交互性。

1. Python Bokeh库

Bokeh提供简单,灵活和强大的功能,并提供两个界面级别:

  • Bokeh.models:一个低级接口,为应用程序开发人员提供了最大的灵活性。

  • Bokeh.plotting:组成视觉字形的高级界面。

2.Bokeh依赖

在开始Bokeh之前,我们需要在计算机上安装NumPy。

3.安装Bokeh模块

安装bokeh及其依赖项的最简单方法是使用conda或者pip。

要使用conda进行安装,请打开终端并运行以下命令:

sudo conda install bokeh

要使用pip进行安装,请打开终端并运行以下命令:

sudo pip install bokeh

4.验证Bokeh模块的安装

我们可以使用某些命令来验证Bokeh是否已正确安装。
但是,我们将制作一个非常小的程序来提供Bokeh输出以验证其是否正常运行。

from bokeh.plotting import figure, output_file, show

output_file("test.html")
plot = figure()
plot.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2)
show(plot)

这应该在本地创建一个名为test.html的文件,在浏览器中打开该文件,然后看到如下所示的结果:注意,我们仅用很少的几行代码就能创建图形。

5. Python Bokeh示例

既然我们已经验证了Bokeh的安装,现在就可以开始使用它的图形和绘图示例。

5.1)绘制简单的折线图

绘制简单的折线图与我们进行验证的过程非常相似,但是我们将添加一些细节以使该图易于阅读。
让我们看一下代码片段:

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="Temp.", line_width=2)

# show the results
show(p)

让我们看一下该程序的输出:借助figure()函数及其参数,我们还能够为轴提供标题,从而更能说明我们在图上显示的数据以及图例。

5.2)多个图

我们知道如何创建一个简单的绘图,这次我们尝试创建多个绘图。
这是一个示例程序:

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)

让我们看一下该程序的输出:这些图用图例和线条颜色定制得更多。
这样,更容易区分同一张图上的多个折线图。

5.3)矢量化的颜色和大小

当我们需要绘制大量数据时,不同的颜色和大小非常重要,因为我们需要可视化的东西很多,而很少显示。
这是一个示例程序:

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)

让我们看一下该程序的输出:矢量化图在某些情况下非常重要,例如:

  • 显示热图相关数据
  • 显示具有某些参数的密度特性的数据

5.4)链接平移和刷

链接各个方面是用于数据可视化的非常有用的技术。
这是一个示例程序,如何使用Bokeh实现:

import numpy as np
from bokeh.layouts import gridplot
from bokeh.plotting import figure, output_file, show

# prepare some data
N = 100
x = np.linspace(0, 4*np.pi, N)
y0 = np.sin(x)
y1 = np.cos(x)
y2 = np.sin(x) + np.cos(x)

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

# create a new plot
s1 = figure(width=250, plot_height=250, title=None)
s1.circle(x, y0, size=10, color="navy", alpha=0.5)

# NEW: create a new plot and share both ranges
s2 = figure(width=250, height=250, x_range=s1.x_range, y_range=s1.y_range, title=None)
s2.triangle(x, y1, size=10, color="firebrick", alpha=0.5)

# NEW: create a new plot and share only one range
s3 = figure(width=250, height=250, x_range=s1.x_range, title=None)
s3.square(x, y2, size=10, color="olive", alpha=0.5)

# NEW: put the subplots in a gridplot
p = gridplot([[s1, s2, s3]], toolbar_location=None)

# show the results
show(p)

让我们看一下该程序的输出:当我们需要显示一个参数基于另一个参数的变化时,这种图特别有用。

5.5)日期时间轴

使用datetime进行绘图是非常常见的任务。
让我们尝试一个示例程序:

import numpy as np

from bokeh.plotting import figure, output_file, show
from bokeh.sampledata.stocks import AAPL

# prepare some data
aapl = np.array(AAPL['adj_close'])
aapl_dates = np.array(AAPL['date'], dtype=np.datetime64)
window_size = 30
window = np.ones(window_size)/float(window_size)
aapl_avg = np.convolve(aapl, window, 'same')

# output to static HTML file
output_file("stocks.html", title="stocks.py example")

# create a new plot with a a datetime axis type
p = figure(width=800, height=350, x_axis_type="datetime")

# add renderers
p.circle(aapl_dates, aapl, size=4, color='darkgrey', alpha=0.2, legend='close')
p.line(aapl_dates, aapl_avg, color='navy', legend='avg')

# NEW: customize by setting attributes
p.title.text = "AAPL One-Month Average"
p.legend.location = "top_left"
p.grid.grid_line_alpha=0
p.xaxis.axis_label = 'Date'
p.yaxis.axis_label = 'Price'
p.ygrid.band_fill_color="olive"
p.ygrid.band_fill_alpha = 0.1

# show the results
show(p)