自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Bokeh,一個(gè)超強(qiáng)交互式Python可視化庫(kù)!

開發(fā) 后端
Bokeh是一款交互式可視化庫(kù),在瀏覽器上進(jìn)行展示。一起來看看吧。

 Bokeh簡(jiǎn)介

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

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

安裝

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

  1. conda install bokeh 

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

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

  1. 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ù)。 

  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模塊繪圖的流程。

一般來說,我們使用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)格: 

  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=TOOLSx_range=(0, 100), y_range=(0, 100))  
  16. # 添加圓繪圖渲染函數(shù),并且定義元素的顏色、樣式  
  17. p.circle(x, y, radius=radiifill_color=colorsfill_alpha=0.6, line_color=None 
  18. # 顯示圖表  
  19. show(p) 

對(duì)于同一個(gè)數(shù)據(jù),可能需要多種展示風(fēng)格,比如說線、點(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=250plot_height=250title=None 
  14. s1.circle(x, y0, size=10color="navy"alpha=0.5)   
  15. # 創(chuàng)建子圖表2,元素樣式為三角形  
  16. s2 = figure(width=250height=250x_range=s1.x_range, y_range=s1.y_range, title=None 
  17. s2.triangle(x, y1, size=10color="firebrick"alpha=0.5)  
  18. # 創(chuàng)建子圖表3,元素樣式為正方形  
  19. s3 = figure(width=250height=250x_range=s1.x_range, title=None 
  20. s3.square(x, y2, size=10color="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=800plot_height=350x_axis_type="datetime" 
  14. # 添加圖表渲染  
  15. p.circle(aapl_dates, aapl, size=4color='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)大之處,去探索更多的用法。 

 

責(zé)任編輯:龐桂玉 來源: Python編程
相關(guān)推薦

2011-06-13 18:54:12

2024-08-02 10:30:39

StreamlitPython庫(kù)數(shù)據(jù)驅(qū)動(dòng)

2020-06-18 10:02:25

Python 開發(fā)編程語(yǔ)言

2023-12-18 15:02:00

PyechartsPython數(shù)據(jù)可視化工具

2015-10-14 17:59:53

Google數(shù)據(jù)探索交互開發(fā)

2020-12-20 17:40:04

機(jī)器學(xué)習(xí)可視化網(wǎng)站算法

2017-01-05 15:06:23

2020-12-31 10:29:05

數(shù)據(jù)可視化可視化工具編碼

2020-04-06 20:47:42

FishShellLinux

2024-03-13 00:00:01

可視化技術(shù)氣泡圖

2020-12-11 08:00:00

數(shù)據(jù)可視化工具大數(shù)據(jù)

2016-11-29 12:25:56

Python大數(shù)據(jù)數(shù)據(jù)可視化

2020-07-27 07:37:43

Python開發(fā)工具

2017-06-19 08:30:35

大數(shù)據(jù)數(shù)據(jù)可視化報(bào)表

2017-02-17 09:20:24

rtopSSH監(jiān)控

2024-03-07 12:53:00

大數(shù)據(jù)組件

2018-05-08 08:35:34

LinuxDocker 容器管理器

2025-04-29 09:26:34

Orange交互式數(shù)據(jù)挖掘機(jī)器學(xué)習(xí)

2021-02-20 09:14:35

PythonPygal可視化

2022-08-26 09:15:58

Python可視化plotly
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)