bokeh python_Python Bokeh数据可视化教程

bokeh python

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

Bokeh是一个交互式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 NumPy installed on our machine.

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

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

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

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

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, show

output_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 data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

# output to static HTML file
output_file("lines.html")

# create a new plot with a title and axis labels
p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')

# add a line renderer with legend and line thickness
p.line(x, y, legend="Temp.", line_width=2)

# show the results
show(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 data
x = [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 file
output_file("log_lines.html")

# create a new plot
p = 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 renderers
p.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 results
show(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 np
from bokeh.plotting import figure, output_file, show

# prepare some data
N = 4000
x = np.random.random(size=N) * 100
y = np.random.random(size=N) * 100
radii = np.random.random(size=N) * 1.5
colors = [
    "#%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 ranges
p = figure(tools=TOOLS, x_range=(0,100), y_range=(0,100))

# add a circle renderer with vectorized colors and sizes
p.circle(x,y, radius=radii, fill_color=colors, fill_alpha=0.6, line_color=None)

# show the results
show(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 np
from bokeh.layouts import gridplot
from bokeh.plotting import figure, output_file, show

# prepare some data
N = 100
x = 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 file
output_file("linked_panning.html")

# create a new plot
s1 = 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 ranges
s2 = 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 range
s3 = 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 gridplot
p = gridplot([[s1, s2, s3]], toolbar_location=None)

# show the results
show(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 np

from bokeh.plotting import figure, output_file, show
from bokeh.sampledata.stocks import AAPL

# prepare some data
aapl = np.array(AAPL['adj_close'])
aapl_dates = np.array(AAPL['date'], dtype=np.datetime64)
window_size = 30
window = np.ones(window_size)/float(window_size)
aapl_avg = np.convolve(aapl, window, 'same')

# output to static HTML file
output_file("stocks.html", title="stocks.py example")

# create a new plot with a a datetime axis type
p = figure(width=800, height=350, x_axis_type="datetime")

# add renderers
p.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 attributes
p.title.text = "AAPL One-Month Average"
p.legend.location = "top_left"
p.grid.grid_line_alpha=0
p.xaxis.axis_label = 'Date'
p.yaxis.axis_label = 'Price'
p.ygrid.band_fill_color="olive"
p.ygrid.band_fill_alpha = 0.1

# show the results
show(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使得吸引人的数据可视化变得容易,并且易于阅读和理解。

翻译自: https://www.journaldev.com/19527/bokeh-python-data-visualization

bokeh python

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值