博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
bokeh python_Python Bokeh数据可视化教程
阅读量:2533 次
发布时间:2019-05-11

本文共 8588 字,大约阅读时间需要 28 分钟。

bokeh python

is an interactive Python data visualization library which targets modern web browsers for presentation.

是一个交互式Python数据可视化库,以现代Web浏览器为对象进行演示。

Python Bokeh library aims at providing high-performing interactivity with the concise construction of novel graphics over very large or even streaming datasets in a quick, easy way and elegant manner.

Python Bokeh库旨在以快速,便捷的方式和优雅的方式,在非常大的甚至是流的数据集上以新颖的图形的简洁结构提供高性能的交互性。

1. Python Bokeh库 (1. Python Bokeh Library)

Bokeh offers simple, flexible and powerful features and provides two interface levels:

Bokeh提供简单,灵活和强大的功能,并提供两个界面级别:

  • Bokeh.models: A low-level interface which provides the application developers with most flexibility.

    Bokeh.models :一个低级接口,为应用程序开发人员提供了最大的灵活性。
  • Bokeh.plotting: A higher-level interface to compose visual glyphs.

    Bokeh.plotting :组成可视字形的高级界面。

2.散景依赖 (2. Bokeh Dependencies)

Before beginning with Bokeh, we need to have installed on our machine.

在开始Bokeh之前,我们需要在计算机上安装 。

3.安装散景模块 (3. Installing Bokeh Module)

The easiest way to install bokeh and its dependencies is by using conda or .

安装bokeh及其依赖项的最简单方法是使用conda或 。

To install using conda open the terminal and run the following command:

要使用conda进行安装,请打开终端并运行以下命令:

sudo conda install bokeh

To install using pip open the terminal and run the following command:

要使用pip进行安装,请打开终端并运行以下命令:

sudo pip install bokeh

4.验证Bokeh模块的安装 (4. Verifying Installation of Bokeh Module)

We can verify that Bokeh is correctly installed or not by using some commands. But we will instead make a very small program to provide Bokeh output to verify that it is working properly.

我们可以使用某些命令来验证Bokeh是否已正确安装。 但是,我们将制作一个非常小的程序来提供Bokeh输出以验证其是否正常运行。

from bokeh.plotting import figure, output_file, showoutput_file("test.html")plot = figure()plot.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2)show(plot)

This should create a file named test.html locally, open that file in browser and see the results like this:

Bokeh python example
Notice how we were able to create a graph by just using very few lines of code.

这应该在本地创建一个名为test.html的文件,在浏览器中打开该文件,然后看到如下结果:

请注意,我们仅用很少的几行代码就能创建图形。

5. Python散景示例 (5. Python Bokeh Examples)

Now that we have verified Bokeh installation, we can get started with its examples of graphs and plots.

既然我们已经验证了Bokeh的安装,现在就可以开始使用它的图形和绘图示例。

5.1)绘制简单的折线图 (5.1) Plotting a simple line graph)

Plotting a simple line graph is quite similar to what we did for verification, but we are going to add a few details to make the plot easy to read. Let’s look at a code snippet:

绘制简单的折线图与我们进行验证的过程非常相似,但是我们将添加一些细节以使该图易于阅读。 让我们看一下代码片段:

from bokeh.plotting import figure, output_file, show# prepare some datax = [1, 2, 3, 4, 5]y = [6, 7, 2, 4, 5]# output to static HTML fileoutput_file("lines.html")# create a new plot with a title and axis labelsp = figure(title="simple line example", x_axis_label='x', y_axis_label='y')# add a line renderer with legend and line thicknessp.line(x, y, legend="Temp.", line_width=2)# show the resultsshow(p)

Let’s see the output for this program:

bokeh simple line graph
With the
figure() function and its parameters, we were able to provide titles for the axes as well which is much more descriptive about what data we are presenting on the graph along with the graph legends.

让我们看一下该程序的输出:

借助figure()函数及其参数,我们还能够为轴提供标题,从而更能说明我们在图上显示的数据以及图例。

5.2)多个图 (5.2) Multiple Plots)

We know how to create a simple plot, let’s try creating multiple plots this time. Here is a sample program:

我们知道如何创建一个简单的图,这次我们尝试创建多个图。 这是一个示例程序:

from bokeh.plotting import figure, output_file, show# prepare some datax = [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]# output to static HTML fileoutput_file("log_lines.html")# create a new plotp = 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')# add some renderersp.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 the resultsshow(p)

Let’s see the output for this program:

bokeh multiple line graphs
These graph plots were much more customised with the legends and line colors. This way, it is easier to differentiate between multiple line plots on the same graph.

让我们看一下该程序的输出:

这些图用图例和线条颜色定制得多。 这样,更容易区分同一图形上的多个折线图。

5.3)矢量化的颜色和大小 (5.3) Vectorized Colors And Sizes)

Different colors and sizes are very important when we need to plot large data as we have a lot to visualize and very few to show. Here is a sample program:

当我们需要绘制大数据时,不同的颜色和大小非常重要,因为我们需要可视化的东西很多,而很少显示。 这是一个示例程序:

import numpy as npfrom bokeh.plotting import figure, output_file, show# prepare some dataN = 4000x = np.random.random(size=N) * 100y = np.random.random(size=N) * 100radii = np.random.random(size=N) * 1.5colors = [    "#%02x%02x%02x" % (int(r), int(g), 150) for r, g in zip(50+2*x, 30+2*y)]# output to static HTML file (with CDN resources)output_file("color_scatter.html", title="color_scatter.py example", mode="cdn")TOOLS="crosshair,pan,wheel_zoom,box_zoom,reset,box_select,lasso_select"# create a new plot with the tools above, and explicit rangesp = figure(tools=TOOLS, x_range=(0,100), y_range=(0,100))# add a circle renderer with vectorized colors and sizesp.circle(x,y, radius=radii, fill_color=colors, fill_alpha=0.6, line_color=None)# show the resultsshow(p)

Let’s see the output for this program:

bokeh vector color
Vectorized graphs are very important in some scenarios, like:

让我们看一下该程序的输出:

矢量化图在某些情况下非常重要,例如:

  • Showing heatmap related data

    显示热图相关数据
  • Showing data which exhibits the density property of some parameters

    显示具有某些参数的密度特性的数据

5.4)链接平移和刷 (5.4) Linked panning and brushing)

Linking various aspects is a very useful technique for data visualization. Here is a sample program how this can be achieved with Bokeh:

链接各个方面是用于数据可视化的非常有用的技术。 这是一个示例程序,如何使用Bokeh实现此目的:

import numpy as npfrom bokeh.layouts import gridplotfrom bokeh.plotting import figure, output_file, show# prepare some dataN = 100x = np.linspace(0, 4*np.pi, N)y0 = np.sin(x)y1 = np.cos(x)y2 = np.sin(x) + np.cos(x)# output to static HTML fileoutput_file("linked_panning.html")# create a new plots1 = figure(width=250, plot_height=250, title=None)s1.circle(x, y0, size=10, color="navy", alpha=0.5)# NEW: create a new plot and share both rangess2 = 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)# NEW: create a new plot and share only one ranges3 = figure(width=250, height=250, x_range=s1.x_range, title=None)s3.square(x, y2, size=10, color="olive", alpha=0.5)# NEW: put the subplots in a gridplotp = gridplot([[s1, s2, s3]], toolbar_location=None)# show the resultsshow(p)

Let’s see the output for this program:

bokeh planning brushing
These kind of plots are especially helpful when we need to show variation of a parameter based on another parameter.

让我们看一下该程序的输出:

当我们需要显示一个参数基于另一个参数的变化时,此类图特别有用。

5.5)日期时间轴 (5.5) Datetime axes)

Plotting with datetime is a very common task. Let’s make an attempt with a sample program:

使用datetime进行绘图是非常常见的任务。 让我们尝试一个示例程序:

import numpy as npfrom bokeh.plotting import figure, output_file, showfrom bokeh.sampledata.stocks import AAPL# prepare some dataaapl = np.array(AAPL['adj_close'])aapl_dates = np.array(AAPL['date'], dtype=np.datetime64)window_size = 30window = np.ones(window_size)/float(window_size)aapl_avg = np.convolve(aapl, window, 'same')# output to static HTML fileoutput_file("stocks.html", title="stocks.py example")# create a new plot with a a datetime axis typep = figure(width=800, height=350, x_axis_type="datetime")# add renderersp.circle(aapl_dates, aapl, size=4, color='darkgrey', alpha=0.2, legend='close')p.line(aapl_dates, aapl_avg, color='navy', legend='avg')# NEW: customize by setting attributesp.title.text = "AAPL One-Month Average"p.legend.location = "top_left"p.grid.grid_line_alpha=0p.xaxis.axis_label = 'Date'p.yaxis.axis_label = 'Price'p.ygrid.band_fill_color="olive"p.ygrid.band_fill_alpha = 0.1# show the resultsshow(p)

Let’s see the output for this program:

bokeh datetime axes

让我们看一下该程序的输出:

六,结论 (6. Conclusion)

In this tutorial, we have seen that Bokeh makes it easy to visualize large data and create different graph plots. We have seen examples of different types of graphs. Bokeh makes it easy to visualize data attractively and make it easier to read and understand.

在本教程中,我们已经看到Bokeh可以轻松地可视化大数据并创建不同的图形图。 我们已经看到了不同类型图的示例。 Bokeh使得吸引人的数据可视化变得容易,并且易于阅读和理解。

翻译自:

bokeh python

转载地址:http://kuqzd.baihongyu.com/

你可能感兴趣的文章
蚂蚁金服井贤栋:用技术联手金融机构,形成服务小微的生态合力
查看>>
端口号大全
查看>>
机器学习基石笔记2——在何时可以使用机器学习(2)
查看>>
POJ 3740 Easy Finding (DLX模板)
查看>>
MySQL 处理重复数据
查看>>
关于typedef的用法总结(转)
查看>>
【strtok()】——分割字符串
查看>>
Linux下安装rabbitmq
查看>>
曹德旺
查看>>
【转】判断点在多边形内(matlab)
查看>>
java基础之集合:List Set Map的概述以及使用场景
查看>>
Python 线程 进程 协程
查看>>
iOS语言中的KVO机制
查看>>
excel第一次打开报错 向程序发送命令时出错 多种解决办法含终极解决方法
查看>>
响应式web设计之CSS3 Media Queries
查看>>
实验三
查看>>
机器码和字节码
查看>>
环形菜单的实现
查看>>
【解决Chrome浏览器和IE浏览器上传附件兼容的问题 -- Chrome关闭flash后,uploadify插件不可用的解决办法】...
查看>>
34 帧动画
查看>>