日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

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

新聞中心

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

 Bokeh簡介

Bokeh是一款交互式可視化庫,在瀏覽器上進行展示。

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

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

安裝

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

 
 
 
 
  1. conda install bokeh 

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

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

 
 
 
 
  1. pip install bokeh 

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

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

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

開始繪圖

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

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

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

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

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

上面的例子繪制了一個折線圖,簡單地展示了bokeh.plotting模塊繪圖的流程。

一般來說,我們使用bokeh.plotting模塊繪圖有以下幾個步驟:

  •  準備數(shù)據(jù)

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

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

          上面說過,圖表輸出有兩種形式,一個是在notebook中直接顯示,一個是生成HTML文件,在瀏覽器中自動打開。

  •  調(diào)用figure()函數(shù)

           創(chuàng)建具有典型默認選項并易于自定義標題、工具和軸標簽的圖表

  •  添加渲染器

          上面使用的是line()線圖函數(shù),并且指定了數(shù)據(jù)源、線條樣式、標簽等,你也可以使用其他的繪圖函數(shù),如點圖、柱狀圖等

  •  顯示或保存圖表

           show()函數(shù)用來自動打開生成的HTML文件,save()函數(shù)用來保存生成的html文件

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

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

 
 
 
 
  1. from bokeh.plotting import figure, output_notebook, show  
  2. # 準備三個數(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ù)據(jù)點在x、y軸的位置,而且要賦予數(shù)據(jù)點顏色、大小等屬性,展示數(shù)據(jù)點的其它含義,如下:

 
 
 
 
  1. import numpy as np  
  2. from bokeh.plotting import figure, output_file, show  
  3. # 準備數(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)建圖表,并添加圖標欄工具  
  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) 

對于同一個數(shù)據(jù),可能需要多種展示風格,比如說線、點、圓等,并且把多個圖表放在一起,Bokeh能夠做到:

 
 
 
 
  1. import numpy as np  
  2. from bokeh.layouts import gridplot  
  3. from bokeh.plotting import figure, output_file, show  
  4. # 準備數(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. # 將多個子圖放到網(wǎng)格圖中  
  22. p = gridplot([[s1, s2, s3]], toolbar_location=None)  
  23. # 顯示圖表  
  24. show(p) 

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

 
 
 
 
  1. import numpy as np  
  2. from bokeh.plotting import figure, output_file, show  
  3. from bokeh.sampledata.stocks import AAPL  
  4. # 準備數(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é)

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


新聞標題:Bokeh,一個超強交互式Python可視化庫!
分享地址:http://www.5511xx.com/article/djciosg.html