Bokeh,一个超强交互式Python可视化库!

 Bokeh简介

Bokeh是一款交互式可视化库,在浏览器上进行展示。

10年积累的网站制作、成都网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有清河门免费网站建设让你可以放心的选择与我们合作。

Bokeh可以通过Python(或其它语言),快速便捷地为大型流数据集提供优雅简洁的高性能交互式图表。

安装

在python中有多种安装Bokeh的方法,这里建议最简单的方法是使用Anaconda Python发行版,然后在命令行下输入以下命令:

 
 
 
 
  1. conda install bokeh 

这里会安装Bokeh需要的所有依赖包,并且Anaconda可以最大限度地减少安装过程的复杂程度。

如果你自信已经安装好需要的依赖,如numpy等,那么可以在命令行使用pip来安装:

 
 
 
 
  1. pip install bokeh 

为什么使用jupyter notebook作为绘图环境

本文代码都是在notebook中执行的,并且图表也直接展示在notebook中。

notebook是用于数据探索的常用工具,在数据科学领域被广泛使用,建议大家在学习Bokeh的过程中使用jupyter notebook。

开始绘图

Bokeh是一个大型库,具有非常多的功能,这里不细讲具体函数方法,只通过一些案例来展示Bokeh的使用流程和可视化界面。

将python列表中的数据绘制成线图非常简单,而且图表是交互式的,能够缩放、平移、保存等其他功能。

图表最终会保存为html格式,并在浏览器中自动打开,这可以通过output_file()函数实现。

如果你使用的是notebook环境,Bokeh可以在notebook中直接显示交互式图表,只要将output_file()函数替换为output_notebook()函数。

 
 
 
 
  1. # 导入相关库  
  2. from bokeh.plotting import figure, output_notebook, show   
  3. % matplotlib inline  
  4. # 准备数据  
  5. x = [1, 2, 3, 4, 5]  
  6. y = [6, 7, 2, 4, 5]  
  7. # 在notbook中展示  
  8. output_notebook()  
  9. # 创建一个带有标题和轴标签的新图表  
  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模块绘图有以下几个步骤:

  •  准备数据

          例子中数据容器为列表,你也可以用numpy array、pandas series数据形式

  •  告诉Bokeh在哪生成输出图表

          上面说过,图表输出有两种形式,一个是在notebook中直接显示,一个是生成HTML文件,在浏览器中自动打开。

  •  调用figure()函数

           创建具有典型默认选项并易于自定义标题、工具和轴标签的图表

  •  添加渲染器

          上面使用的是line()线图函数,并且指定了数据源、线条样式、标签等,你也可以使用其他的绘图函数,如点图、柱状图等

  •  显示或保存图表

           show()函数用来自动打开生成的HTML文件,save()函数用来保存生成的html文件

如果想在一张图里绘制多个数据表,则可以重复上面第4步。

你可以添加多个数据系列,自定义不同的展示风格:

 
 
 
 
  1. from bokeh.plotting import figure, output_notebook, show  
  2. # 准备三个数据系列  
  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. # 创建新表  
  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) 

有时候,绘制图表不光要知道数据点在x、y轴的位置,而且要赋予数据点颜色、大小等属性,展示数据点的其它含义,如下:

 
 
 
 
  1. import numpy as np  
  2. from bokeh.plotting import figure, output_file, show  
  3. # 准备数据  
  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. # 创建图表,并添加图标栏工具  
  15. p = figure(tools=TOOLS, x_range=(0, 100), y_range=(0, 100))  
  16. # 添加圆绘图渲染函数,并且定义元素的颜色、样式  
  17. p.circle(x, y, radius=radii, fill_color=colors, fill_alpha=0.6, line_color=None)  
  18. # 显示图表  
  19. show(p) 

对于同一个数据,可能需要多种展示风格,比如说线、点、圆等,并且把多个图表放在一起,Bokeh能够做到:

 
 
 
 
  1. import numpy as np  
  2. from bokeh.layouts import gridplot  
  3. from bokeh.plotting import figure, output_file, show  
  4. # 准备数据  
  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. # 创建子图表1,元素样式为圆  
  13. s1 = figure(width=250, plot_height=250, title=None)  
  14. s1.circle(x, y0, size=10, color="navy", alpha=0.5)   
  15. # 创建子图表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. # 创建子图表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. # 将多个子图放到网格图中  
  22. p = gridplot([[s1, s2, s3]], toolbar_location=None)  
  23. # 显示图表  
  24. show(p) 

绘制股票价格走势图,这类是关于时间序列的图表:

 
 
 
 
  1. import numpy as np  
  2. from bokeh.plotting import figure, output_file, show  
  3. from bokeh.sampledata.stocks import AAPL  
  4. # 准备数据  
  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. # 创建新图表  
  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. # 设置图表元素  
  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) 

总结

上述几个示例简单展示了Bokeh绘图方法,希望起到一个抛砖引玉的作用,让大家了解到Bokeh的强大之处,去探索更多的用法。

当前题目:Bokeh,一个超强交互式Python可视化库!
网址分享:http://www.hantingmc.com/qtweb/news36/38886.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联