Bokeh,一個(gè)超強(qiáng)交互式Python可視化庫(kù)!
Bokeh簡(jiǎn)介
Bokeh是一款交互式可視化庫(kù),在瀏覽器上進(jìn)行展示。
Bokeh可以通過Python(或其它語(yǔ)言),快速便捷地為大型流數(shù)據(jù)集提供優(yōu)雅簡(jiǎn)潔的高性能交互式圖表。
安裝
在python中有多種安裝Bokeh的方法,這里建議最簡(jiǎn)單的方法是使用Anaconda Python發(fā)行版,然后在命令行下輸入以下命令:
- conda install bokeh
這里會(huì)安裝Bokeh需要的所有依賴包,并且Anaconda可以最大限度地減少安裝過程的復(fù)雜程度。
如果你自信已經(jīng)安裝好需要的依賴,如numpy等,那么可以在命令行使用pip來安裝:
- pip install bokeh
為什么使用jupyter notebook作為繪圖環(huán)境
本文代碼都是在notebook中執(zhí)行的,并且圖表也直接展示在notebook中。
notebook是用于數(shù)據(jù)探索的常用工具,在數(shù)據(jù)科學(xué)領(lǐng)域被廣泛使用,建議大家在學(xué)習(xí)Bokeh的過程中使用jupyter notebook。
開始繪圖
Bokeh是一個(gè)大型庫(kù),具有非常多的功能,這里不細(xì)講具體函數(shù)方法,只通過一些案例來展示Bokeh的使用流程和可視化界面。
將python列表中的數(shù)據(jù)繪制成線圖非常簡(jiǎn)單,而且圖表是交互式的,能夠縮放、平移、保存等其他功能。
圖表最終會(huì)保存為html格式,并在瀏覽器中自動(dòng)打開,這可以通過output_file()函數(shù)實(shí)現(xiàn)。
如果你使用的是notebook環(huán)境,Bokeh可以在notebook中直接顯示交互式圖表,只要將output_file()函數(shù)替換為output_notebook()函數(shù)。
- # 導(dǎo)入相關(guān)庫(kù)
- from bokeh.plotting import figure, output_notebook, show
- % matplotlib inline
- # 準(zhǔn)備數(shù)據(jù)
- x = [1, 2, 3, 4, 5]
- y = [6, 7, 2, 4, 5]
- # 在notbook中展示
- output_notebook()
- # 創(chuàng)建一個(gè)帶有標(biāo)題和軸標(biāo)簽的新圖表
- p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')
- # 添加帶有圖例和線條粗細(xì)的線圖渲染器
- #
- p.line(x, y, legend="Temp.", line_width=2)
- # 顯示圖表
- show(p)
上面的例子繪制了一個(gè)折線圖,簡(jiǎn)單地展示了bokeh.plotting模塊繪圖的流程。
一般來說,我們使用bokeh.plotting模塊繪圖有以下幾個(gè)步驟:
- 準(zhǔn)備數(shù)據(jù)
例子中數(shù)據(jù)容器為列表,你也可以用numpy array、pandas series數(shù)據(jù)形式
- 告訴Bokeh在哪生成輸出圖表
上面說過,圖表輸出有兩種形式,一個(gè)是在notebook中直接顯示,一個(gè)是生成HTML文件,在瀏覽器中自動(dòng)打開。
- 調(diào)用figure()函數(shù)
創(chuàng)建具有典型默認(rèn)選項(xiàng)并易于自定義標(biāo)題、工具和軸標(biāo)簽的圖表
- 添加渲染器
上面使用的是line()線圖函數(shù),并且指定了數(shù)據(jù)源、線條樣式、標(biāo)簽等,你也可以使用其他的繪圖函數(shù),如點(diǎn)圖、柱狀圖等
- 顯示或保存圖表
show()函數(shù)用來自動(dòng)打開生成的HTML文件,save()函數(shù)用來保存生成的html文件
如果想在一張圖里繪制多個(gè)數(shù)據(jù)表,則可以重復(fù)上面第4步。
你可以添加多個(gè)數(shù)據(jù)系列,自定義不同的展示風(fēng)格:
- from bokeh.plotting import figure, output_notebook, show
- # 準(zhǔn)備三個(gè)數(shù)據(jù)系列
- 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]
- # 在notbook中展示
- output_notebook()
- # 創(chuàng)建新表
- 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'
- )
- # 添加不同的圖表渲染
- 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(p)
有時(shí)候,繪制圖表不光要知道數(shù)據(jù)點(diǎn)在x、y軸的位置,而且要賦予數(shù)據(jù)點(diǎn)顏色、大小等屬性,展示數(shù)據(jù)點(diǎn)的其它含義,如下:
- import numpy as np
- from bokeh.plotting import figure, output_file, show
- # 準(zhǔn)備數(shù)據(jù)
- 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)
- ]
- # 在notbook中展示
- output_notebook()
- TOOLS = "crosshair,pan,wheel_zoom,box_zoom,reset,box_select,lasso_select"
- # 創(chuàng)建圖表,并添加圖標(biāo)欄工具
- p = figure(tools=TOOLS, x_range=(0, 100), y_range=(0, 100))
- # 添加圓繪圖渲染函數(shù),并且定義元素的顏色、樣式
- p.circle(x, y, radius=radii, fill_color=colors, fill_alpha=0.6, line_color=None)
- # 顯示圖表
- show(p)
對(duì)于同一個(gè)數(shù)據(jù),可能需要多種展示風(fēng)格,比如說線、點(diǎn)、圓等,并且把多個(gè)圖表放在一起,Bokeh能夠做到:
- import numpy as np
- from bokeh.layouts import gridplot
- from bokeh.plotting import figure, output_file, show
- # 準(zhǔn)備數(shù)據(jù)
- 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)
- # 在notbook中展示
- output_notebook()
- # 創(chuàng)建子圖表1,元素樣式為圓
- s1 = figure(width=250, plot_height=250, title=None)
- s1.circle(x, y0, size=10, color="navy", alpha=0.5)
- # 創(chuàng)建子圖表2,元素樣式為三角形
- 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)
- # 創(chuàng)建子圖表3,元素樣式為正方形
- s3 = figure(width=250, height=250, x_range=s1.x_range, title=None)
- s3.square(x, y2, size=10, color="olive", alpha=0.5)
- # 將多個(gè)子圖放到網(wǎng)格圖中
- p = gridplot([[s1, s2, s3]], toolbar_location=None)
- # 顯示圖表
- show(p)
繪制股票價(jià)格走勢(shì)圖,這類是關(guān)于時(shí)間序列的圖表:
- import numpy as np
- from bokeh.plotting import figure, output_file, show
- from bokeh.sampledata.stocks import AAPL
- # 準(zhǔn)備數(shù)據(jù)
- 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')
- # 在notbook中展示
- output_notebook()
- # 創(chuàng)建新圖表
- p = figure(plot_width=800, plot_height=350, x_axis_type="datetime")
- # 添加圖表渲染
- p.circle(aapl_dates, aapl, size=4, color='darkgrey', alpha=0.2, legend='close')
- p.line(aapl_dates, aapl_avg, color='navy', legend='avg')
- # 設(shè)置圖表元素
- 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(p)
總結(jié)
上述幾個(gè)示例簡(jiǎn)單展示了Bokeh繪圖方法,希望起到一個(gè)拋磚引玉的作用,讓大家了解到Bokeh的強(qiáng)大之處,去探索更多的用法。