通過“中級”接口 - bokeh.plotting喝滞,介紹Bokeh的可視化過程。
最基礎(chǔ)的編寫包含如下三個部分
- 導(dǎo)入
- 定義畫布膏秫,繪畫
- 輸出
樣例1:
1右遭、導(dǎo)入
from bokeh.io import output_file, show
from bokeh.plotting import figure
plotting
可以理解為一堆用于作圖的工具, 而figure
就是其中的一頁用于作圖的紙
output_file
用于指定輸出到的文件。 如果使用Jupyter Notebook/ Lab的話缤削, 使用output_notebook
更合適窘哈。
show
則是代碼層面告訴程序, 我已經(jīng)畫完了亭敢, 可以輸出了的指令滚婉。
2、 定義畫布帅刀, 繪畫让腹。 該案例為一張“散點(diǎn)圖”
p = figure(plot_width=400, plot_height=400)
p.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=15, line_color="navy", fill_color="orange", fill_alpha=0.5)
通過
figure(plot_width=400, plot_height=400)
定義好一個400x400的一頁畫布, 取個名字叫p
通過定義一個“記號”(glyph)p.circle
告訴程序扣溺, 我想用圓來做標(biāo)記骇窍。
circle
參數(shù)
[1, 2, 3, 4, 5], [6, 7, 2, 4, 5]
橫坐標(biāo),縱坐標(biāo)
size=15
Bokeh的所有散列標(biāo)記均接受size
參數(shù)锥余, 指定散列標(biāo)記的大小像鸡, 該大小以背景空間的單個元素大小作為單位。該案例中哈恰, 特別的由于標(biāo)記點(diǎn)為圓形只估, 所以也接受着绷。redius
作為參數(shù), 定義標(biāo)點(diǎn)的半徑大小
3蛔钙、 輸出
output_file("file_name.html")
show(p)
矩形散列圖
如果想用矩形作為標(biāo)記作圖, 那么使用square
方法即可荠医。
# create a new plot using figure
p = figure(plot_width=400, plot_height=400)
# add a square renderer with a size, color, alpha, and sizes
p.square([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=[10, 15, 20, 25, 30], color="firebrick", alpha=0.6)
show(p) # show the results
注意到上面的例子中吁脱, 我們針對每一個獨(dú)立的標(biāo)記設(shè)定了大小桑涎。
線條圖
# create a new plot (with a title) using figure
p = figure(plot_width=400, plot_height=400, title="My Line Plot")
# add a line renderer
p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2)
show(p) # show the results
線條圖案中除了line_width
參數(shù)以外, 還有例如line_color
或者line_dash
參數(shù)可以設(shè)定兼贡。
時間坐標(biāo)
本案例為攻冷,葡萄糖含量隨時間的變化
from bokeh.plotting import figure
from bokeh.io import output_file, show
from bokeh.sampledata.glucose import data
data.head()
# reduce data size to one week
week = data.loc['2010-10-01':'2010-10-08']
p = figure(x_axis_type="datetime", title="Glocose Range", plot_height=350, plot_width=800)
p.xgrid.grid_line_color=None
p.ygrid.grid_line_alpha=0.5
p.xaxis.axis_label = 'Time'
p.yaxis.axis_label = 'Value'
p.line(week.index, week.glucose)
output_file("time_axes.html")
show(p)
bokeh.sampledata.glucose.data
來源于pandas
下的DataFrame
week = data.loc['2010-10-01':'2010-10-08']
用于創(chuàng)建一個有關(guān)時間的列表, 通過print(week.index)
和print(week.glucose)
我們可以分別看到如下信息:DatetimeIndex(['2010-10-01 00:04:00', '2010-10-01 00:09:00', '2010-10-01 00:14:00', '2010-10-01 00:19:00', '2010-10-01 00:24:00', '2010-10-01 00:29:00', '2010-10-01 00:34:00', '2010-10-01 00:39:00', '2010-10-01 00:44:00', '2010-10-01 00:49:00', ... '2010-10-08 23:12:00', '2010-10-08 23:17:00', '2010-10-08 23:22:00', '2010-10-08 23:27:00', '2010-10-08 23:32:00', '2010-10-08 23:37:00', '2010-10-08 23:42:00', '2010-10-08 23:47:00', '2010-10-08 23:52:00', '2010-10-08 23:57:00'], dtype='datetime64[ns]', name='datetime', length=2217, freq=None)
datetime 2010-10-01 00:04:00 92 2010-10-01 00:09:00 100 2010-10-01 00:14:00 108 2010-10-01 00:19:00 115 2010-10-01 00:24:00 120 ... 2010-10-08 23:37:00 99 2010-10-08 23:42:00 99 2010-10-08 23:47:00 98 2010-10-08 23:52:00 97 2010-10-08 23:57:00 97 Name: glucose, Length: 2217, dtype: int64
Pandas 是一個強(qiáng)大的分析結(jié)構(gòu)化數(shù)據(jù)的工具集。DataFrame和Series都是其厲害的武器遍希。
該案例中等曼,行坐標(biāo)時間由DataFrame快速生成, 列坐標(biāo)通過Series快速生成
本案例中新出現(xiàn)其他內(nèi)容:
figure(x_axis_type="datetime",
定義畫布的x軸坐標(biāo)類型為datatime
凿蒜。
p.xgrid.grid_line_color=None
x軸上的網(wǎng)格線透明禁谦, 即讓網(wǎng)格線中的豎線消失
p.ygrid.grid_line_alpha=0.5
y軸上的網(wǎng)格線半透明,即讓網(wǎng)格線中的橫線以半透明形式展示
六邊形圖案樣例
import numpy as np
from bokeh.plotting import figure
from bokeh.io import output_file, show
from bokeh.palettes import Viridis256
from bokeh.util.hex import hexbin
n = 50000
x = np.random.standard_normal(n)
y = np.random.standard_normal(n)
bins = hexbin(x, y, 0.1)
# color map the bins by hand, will see how to use linear_cmap later
color = [Viridis256[int(i)] for i in bins.counts/max(bins.counts)*255]
# match_aspect ensures neither dimension is squished, regardless of the plot size
p = figure(tools="wheel_zoom,reset", match_aspect=True, background_fill_color='#440154')
p.grid.visible = False
p.hex_tile(bins.q, bins.r, size=0.1, line_color=None, fill_color=color)
output_file("hex_tiling.html")
show(p)
運(yùn)行效果
NumPy 是使用Python進(jìn)行科學(xué)計算的基礎(chǔ)軟件包
Viridis256 是Bokeh提供的一套預(yù)設(shè)(大)調(diào)色板Palettes.PNG
通過修改上面代碼废封, 換一個調(diào)色板
import numpy as np
from bokeh.plotting import figure
from bokeh.io import output_file, show
from bokeh.util.hex import hexbin
# from bokeh.palettes import Viridis256
from bokeh.palettes import Inferno256
n = 50000
x = np.random.standard_normal(n)
y = np.random.standard_normal(n)
bins = hexbin(x, y, size=0.1)
# color = [Viridis256[int(i)] for i in bins.counts/max(bins.counts)*255]
color = [Inferno256[int(i)] for i in bins.counts/max(bins.counts)*255]
# p = figure(tools="wheel_zoom,reset", match_aspect=True, background_fill_color='#440154')
p = figure(tools="wheel_zoom,reset", match_aspect=True, background_fill_color='#000003')
p.grid.visible = False
p.hex_tile(bins.q, bins.r, size=0.1, line_color=None, fill_color=color)
output_file("hex_tiling.html")
show(p)
效果如下
本章節(jié)我們接觸了Bokeh的基本邏輯過程州泊, 包括:
1、導(dǎo)入
2漂洋、定義畫布遥皂、畫散列標(biāo)記或曲線
3、輸出
同時也感受了一下Pandas和NumPy功能的強(qiáng)大刽漂, 為我們學(xué)習(xí)作圖的初期階段提供了巨大的便利