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