一、Bokeh入門

通過“中級”接口 - bokeh.plotting喝滞,介紹Bokeh的可視化過程。

最基礎(chǔ)的編寫包含如下三個部分

  1. 導(dǎo)入
  2. 定義畫布膏秫,繪畫
  3. 輸出

樣例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)  
example

矩形散列圖

如果想用矩形作為標(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è)定了大小桑涎。

example2

線條圖

# 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è)定兼贡。

example2

時間坐標(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)行效果

Hex_Tiling.PNG

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)

效果如下

Hex_Tiling_1.PNG

本章節(jié)我們接觸了Bokeh的基本邏輯過程州泊, 包括:
1、導(dǎo)入
2漂洋、定義畫布遥皂、畫散列標(biāo)記或曲線
3、輸出

同時也感受了一下Pandas和NumPy功能的強(qiáng)大刽漂, 為我們學(xué)習(xí)作圖的初期階段提供了巨大的便利

下一篇 二渴肉、Bokeh風(fēng)格與主題

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市爽冕,隨后出現(xiàn)的幾起案子仇祭,更是在濱河造成了極大的恐慌,老刑警劉巖颈畸,帶你破解...
    沈念sama閱讀 212,542評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件乌奇,死亡現(xiàn)場離奇詭異,居然都是意外死亡眯娱,警方通過查閱死者的電腦和手機(jī)礁苗,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,596評論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來徙缴,“玉大人试伙,你說我怎么就攤上這事∮谘” “怎么了疏叨?”我有些...
    開封第一講書人閱讀 158,021評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長穿剖。 經(jīng)常有香客問我蚤蔓,道長,這世上最難降的妖魔是什么糊余? 我笑而不...
    開封第一講書人閱讀 56,682評論 1 284
  • 正文 為了忘掉前任秀又,我火速辦了婚禮单寂,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘吐辙。我一直安慰自己宣决,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,792評論 6 386
  • 文/花漫 我一把揭開白布昏苏。 她就那樣靜靜地躺著尊沸,像睡著了一般。 火紅的嫁衣襯著肌膚如雪捷雕。 梳的紋絲不亂的頭發(fā)上椒丧,一...
    開封第一講書人閱讀 49,985評論 1 291
  • 那天壹甥,我揣著相機(jī)與錄音救巷,去河邊找鬼。 笑死句柠,一個胖子當(dāng)著我的面吹牛浦译,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播溯职,決...
    沈念sama閱讀 39,107評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼精盅,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了谜酒?” 一聲冷哼從身側(cè)響起叹俏,我...
    開封第一講書人閱讀 37,845評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎僻族,沒想到半個月后粘驰,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,299評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡述么,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,612評論 2 327
  • 正文 我和宋清朗相戀三年蝌数,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片度秘。...
    茶點(diǎn)故事閱讀 38,747評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡顶伞,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出剑梳,到底是詐尸還是另有隱情唆貌,我是刑警寧澤,帶...
    沈念sama閱讀 34,441評論 4 333
  • 正文 年R本政府宣布垢乙,位于F島的核電站挠锥,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏侨赡。R本人自食惡果不足惜蓖租,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,072評論 3 317
  • 文/蒙蒙 一粱侣、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蓖宦,春花似錦齐婴、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,828評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至睬关,卻和暖如春诱担,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背电爹。 一陣腳步聲響...
    開封第一講書人閱讀 32,069評論 1 267
  • 我被黑心中介騙來泰國打工蔫仙, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人丐箩。 一個月前我還...
    沈念sama閱讀 46,545評論 2 362
  • 正文 我出身青樓摇邦,卻偏偏與公主長得像,于是被迫代替她去往敵國和親屎勘。 傳聞我的和親對象是個殘疾皇子施籍,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,658評論 2 350

推薦閱讀更多精彩內(nèi)容