日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Bokeh,一個(gè)超強(qiáng)交互式Python可視化庫(kù)!

 Bokeh簡(jiǎn)介

Bokeh是一款交互式可視化庫(kù),在瀏覽器上進(jìn)行展示。

10年積累的網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有清河門免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

Bokeh可以通過(guò)Python(或其它語(yǔ)言),快速便捷地為大型流數(shù)據(jù)集提供優(yōu)雅簡(jiǎn)潔的高性能交互式圖表。

安裝

在python中有多種安裝Bokeh的方法,這里建議最簡(jiǎn)單的方法是使用Anaconda Python發(fā)行版,然后在命令行下輸入以下命令:

 
 
 
 
  1. conda install bokeh 

這里會(huì)安裝Bokeh需要的所有依賴包,并且Anaconda可以最大限度地減少安裝過(guò)程的復(fù)雜程度。

如果你自信已經(jīng)安裝好需要的依賴,如numpy等,那么可以在命令行使用pip來(lái)安裝:

 
 
 
 
  1. pip install bokeh 

為什么使用jupyter notebook作為繪圖環(huán)境

本文代碼都是在notebook中執(zhí)行的,并且圖表也直接展示在notebook中。

notebook是用于數(shù)據(jù)探索的常用工具,在數(shù)據(jù)科學(xué)領(lǐng)域被廣泛使用,建議大家在學(xué)習(xí)Bokeh的過(guò)程中使用jupyter notebook。

開(kāi)始繪圖

Bokeh是一個(gè)大型庫(kù),具有非常多的功能,這里不細(xì)講具體函數(shù)方法,只通過(guò)一些案例來(lái)展示Bokeh的使用流程和可視化界面。

將python列表中的數(shù)據(jù)繪制成線圖非常簡(jiǎn)單,而且圖表是交互式的,能夠縮放、平移、保存等其他功能。

圖表最終會(huì)保存為html格式,并在瀏覽器中自動(dòng)打開(kāi),這可以通過(guò)output_file()函數(shù)實(shí)現(xiàn)。

如果你使用的是notebook環(huán)境,Bokeh可以在notebook中直接顯示交互式圖表,只要將output_file()函數(shù)替換為output_notebook()函數(shù)。

 
 
 
 
  1. # 導(dǎo)入相關(guān)庫(kù)  
  2. from bokeh.plotting import figure, output_notebook, show   
  3. % matplotlib inline  
  4. # 準(zhǔn)備數(shù)據(jù)  
  5. x = [1, 2, 3, 4, 5]  
  6. y = [6, 7, 2, 4, 5]  
  7. # 在notbook中展示  
  8. output_notebook()  
  9. # 創(chuàng)建一個(gè)帶有標(biāo)題和軸標(biāo)簽的新圖表  
  10. p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')  
  11. # 添加帶有圖例和線條粗細(xì)的線圖渲染器  
  12. #   
  13. p.line(x, y, legend="Temp.", line_width=2)  
  14. # 顯示圖表  
  15. show(p) 

上面的例子繪制了一個(gè)折線圖,簡(jiǎn)單地展示了bokeh.plotting模塊繪圖的流程。

一般來(lái)說(shuō),我們使用bokeh.plotting模塊繪圖有以下幾個(gè)步驟:

  •  準(zhǔn)備數(shù)據(jù)

          例子中數(shù)據(jù)容器為列表,你也可以用numpy array、pandas series數(shù)據(jù)形式

  •  告訴Bokeh在哪生成輸出圖表

          上面說(shuō)過(guò),圖表輸出有兩種形式,一個(gè)是在notebook中直接顯示,一個(gè)是生成HTML文件,在瀏覽器中自動(dòng)打開(kāi)。

  •  調(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ù)用來(lái)自動(dòng)打開(kāi)生成的HTML文件,save()函數(shù)用來(lái)保存生成的html文件

如果想在一張圖里繪制多個(gè)數(shù)據(jù)表,則可以重復(fù)上面第4步。

你可以添加多個(gè)數(shù)據(jù)系列,自定義不同的展示風(fēng)格:

 
 
 
 
  1. from bokeh.plotting import figure, output_notebook, show  
  2. # 準(zhǔn)備三個(gè)數(shù)據(jù)系列  
  3. x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]  
  4. y0 = [i**2 for i in x]  
  5. y1 = [10**i for i in x]  
  6. y2 = [10**(i**2) for i in x]  
  7. # 在notbook中展示  
  8. output_notebook()  
  9. # 創(chuàng)建新表  
  10. p = figure(  
  11.    tools="pan,box_zoom,reset,save",  
  12.    y_axis_type="log", y_range=[0.001, 10**11], title="log axis example",  
  13.    x_axis_label='sections', y_axis_label='particles'  
  14. )  
  15. # 添加不同的圖表渲染  
  16. p.line(x, x, legend="y=x")  
  17. p.circle(x, x, legend="y=x", fill_color="white", size=8)  
  18. p.line(x, y0, legend="y=x^2", line_width=3)  
  19. p.line(x, y1, legend="y=10^x", line_color="red")  
  20. p.circle(x, y1, legend="y=10^x", fill_color="red", line_color="red", size=6)  
  21. p.line(x, y2, legend="y=10^x^2", line_color="orange", line_dash="4 4")  
  22. # 展示圖表  
  23. show(p) 

有時(shí)候,繪制圖表不光要知道數(shù)據(jù)點(diǎn)在x、y軸的位置,而且要賦予數(shù)據(jù)點(diǎn)顏色、大小等屬性,展示數(shù)據(jù)點(diǎn)的其它含義,如下:

 
 
 
 
  1. import numpy as np  
  2. from bokeh.plotting import figure, output_file, show  
  3. # 準(zhǔn)備數(shù)據(jù)  
  4. N = 4000  
  5. x = np.random.random(size=N) * 100  
  6. y = np.random.random(size=N) * 100  
  7. radii = np.random.random(size=N) * 1.5  
  8. colors = [  
  9.     "#%02x%02x%02x" % (int(r), int(g), 150) for r, g in zip(50+2*x, 30+2*y) 
  10. ]  
  11. # 在notbook中展示  
  12. output_notebook()   
  13. TOOLS = "crosshair,pan,wheel_zoom,box_zoom,reset,box_select,lasso_select"  
  14. # 創(chuàng)建圖表,并添加圖標(biāo)欄工具  
  15. p = figure(tools=TOOLS, x_range=(0, 100), y_range=(0, 100))  
  16. # 添加圓繪圖渲染函數(shù),并且定義元素的顏色、樣式  
  17. p.circle(x, y, radius=radii, fill_color=colors, fill_alpha=0.6, line_color=None)  
  18. # 顯示圖表  
  19. show(p) 

對(duì)于同一個(gè)數(shù)據(jù),可能需要多種展示風(fēng)格,比如說(shuō)線、點(diǎn)、圓等,并且把多個(gè)圖表放在一起,Bokeh能夠做到:

 
 
 
 
  1. import numpy as np  
  2. from bokeh.layouts import gridplot  
  3. from bokeh.plotting import figure, output_file, show  
  4. # 準(zhǔn)備數(shù)據(jù)  
  5. N = 100  
  6. x = np.linspace(0, 4*np.pi, N) 
  7. y0 = np.sin(x)  
  8. y1 = np.cos(x)  
  9. y2 = np.sin(x) + np.cos(x)  
  10. # 在notbook中展示  
  11. output_notebook()  
  12. # 創(chuàng)建子圖表1,元素樣式為圓  
  13. s1 = figure(width=250, plot_height=250, title=None)  
  14. s1.circle(x, y0, size=10, color="navy", alpha=0.5)   
  15. # 創(chuàng)建子圖表2,元素樣式為三角形  
  16. s2 = figure(width=250, height=250, x_range=s1.x_range, y_range=s1.y_range, title=None)  
  17. s2.triangle(x, y1, size=10, color="firebrick", alpha=0.5)  
  18. # 創(chuàng)建子圖表3,元素樣式為正方形  
  19. s3 = figure(width=250, height=250, x_range=s1.x_range, title=None)  
  20. s3.square(x, y2, size=10, color="olive", alpha=0.5)  
  21. # 將多個(gè)子圖放到網(wǎng)格圖中  
  22. p = gridplot([[s1, s2, s3]], toolbar_location=None)  
  23. # 顯示圖表  
  24. show(p) 

繪制股票價(jià)格走勢(shì)圖,這類是關(guān)于時(shí)間序列的圖表:

 
 
 
 
  1. import numpy as np  
  2. from bokeh.plotting import figure, output_file, show  
  3. from bokeh.sampledata.stocks import AAPL  
  4. # 準(zhǔn)備數(shù)據(jù)  
  5. aapl = np.array(AAPL['adj_close'])  
  6. aapl_dates = np.array(AAPL['date'], dtype=np.datetime64)  
  7. window_size = 30  
  8. window = np.ones(window_size)/float(window_size)  
  9. aapl_avg = np.convolve(aapl, window, 'same')  
  10. # 在notbook中展示  
  11. output_notebook()  
  12. # 創(chuàng)建新圖表  
  13. p = figure(plot_width=800, plot_height=350, x_axis_type="datetime")  
  14. # 添加圖表渲染  
  15. p.circle(aapl_dates, aapl, size=4, color='darkgrey', alpha=0.2, legend='close')  
  16. p.line(aapl_dates, aapl_avg, color='navy', legend='avg')  
  17. # 設(shè)置圖表元素  
  18. p.title.text = "AAPL One-Month Average"  
  19. p.legend.location = "top_left"  
  20. p.grid.grid_line_alpha = 0  
  21. p.xaxis.axis_label = 'Date'  
  22. p.yaxis.axis_label = 'Price'  
  23. p.ygrid.band_fill_color = "olive"  
  24. p.ygrid.band_fill_alpha = 0.1   
  25. # 顯示圖表  
  26. show(p) 

總結(jié)

上述幾個(gè)示例簡(jiǎn)單展示了Bokeh繪圖方法,希望起到一個(gè)拋磚引玉的作用,讓大家了解到Bokeh的強(qiáng)大之處,去探索更多的用法。


網(wǎng)站標(biāo)題:Bokeh,一個(gè)超強(qiáng)交互式Python可視化庫(kù)!
網(wǎng)頁(yè)鏈接:http://www.dlmjj.cn/article/djciosg.html