bokeh python_Python Bokeh数据可视化教程

2023-11-19

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:


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:


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:


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:


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:


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:

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

六,结论 (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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

bokeh python_Python Bokeh数据可视化教程 的相关文章

随机推荐

  • tex 表格中内容左对齐/居中/右对齐

    左对齐是l 右对齐是r 居中是c begin table centering caption label tab widgets Notation summary begin tabular l l 就是这里控制每一列的对齐方式 Notat
  • 史上最简单的SpringCloud教程

    在微服务架构中 需要几个基础的服务治理组件 包括服务注册与发现 服务消费 负载均衡 断路器 智能路由 配置管理等 由这几个基础组件相互协作 共同组建了一个简单的微服务系统 一个简答的微服务系统如下图 注意 A服务和B服务是可以相互调用的 作
  • 异常数据检测

    文章目录 效果一览 文章概述 部分源码 参考资料 效果一览 文章概述 信号分解算法 Matlab基于一维小波分解算法 Wavelet Decomposition 的信号分解算法 部分源码
  • 标准代码书写准则,避免屎山代码风格指南

    牛马程序员 强推 屎山代码风格指南 github 开源地址 https github com trekhleb state of the art shitcode tree master 这是一个你的项目应该遵循的标准代码书写准则的列表 把
  • centos7安装python3.x(多种方式)

    但行好事 莫问前程 有任何疑问请留言 作者有问必答哦 前言 centos系统本身默认安装有python2 x 版本x根据不同版本系统有所不同 可通过 python V 或 python version 查看系统自带的python版本 有一些
  • java POI在excel中插入等比例缩放的图片

    这个缩放的比例不是很准确 但还凑合能用 目前本人找不到其它方法 就先用这个 先看一个关键的API方法 void org apache poi ss usermodel Picture resize double scaleX double
  • DES加解密算法

    DES加解密算法 单密钥对称加解密算法 入口参数有三个 key data mode key为加密解密使用的密钥 data为加密解密的数据 mode为其工作模式 当模式为加密模式时 明文按照64位进行分组 形成明文组 key用于对数据加密 当
  • githubActions部署文件到服务器

    示例 githubAction配置示例 ssh秘钥方式 首先在服务器生成秘钥 参考https github com easingthemes ssh deploy 安装 rsync apt get install rsync 参考 参考 n
  • Go 流程控制 for、for range 循环

    在Go语言中 for循环是一种常用的流程控制语句 可以重复执行一段代码块 直到满足退出条件 同时 Go语言还提供了for range循环 用于遍历数组 切片 映射和字符串等数据结构 在本篇博客中 我们将介绍Go语言中的for循环和for r
  • USART HMI智能串口屏介绍

    概要 USART HMI智能串口屏 该显示屏的介绍 GUI界面的设计 通讯方式和修改控件参数的相关指令等 一开始我们项目组在显示上用的是12864液晶显示屏 带字库 但是看起来效果不是很好 感觉很LOW 而且不知道什么原因 12864常常会
  • Redis主从复制总结整理

    Redis的主从复制策略是通过其持久化的rdb文件来实现的 其过程是先dump出rdb文件 将rdb文件全量传输给slave 然后再将dump后的操作实时同步到slave中 让从服务器 slave server 成为主服务器 master
  • 实现文件里字符替换功能

    思路 首先要打开你要打开的文件例如我这边桌面的demo txt 利用相关函数计算出这个文件大小 然后开始遍历里面的内容 一个字符一个字符的遍历 如果找到了要被替换的字符就当场重新把新的内容赋值进去 最后重新覆盖整个文章 可能表达有误 可直接
  • mysql 5.6 安装流程

    一 首先解压安装包到指定路径 解压路径不可为中文 二 配置环境变量 我是windows11 1 1 2 3 4 5 6 6 全部点击确定 三 更改my ini 这两条路径更改为与环境变量相同路径 四 运行cmd 1 2 输入mysqld i
  • 2021-08-06软考网工的一个简单的综合实验

    拓扑 PC1和PC2都设置成dhcp获取ip PC1属于10网段 标记为教学区 PC2属于20网段 标记为宿舍区 LSW1作为接入交换机 LSW2作为核心交换机 AR1作为外网入口 AR2表示电信运营商的路由器 AR3表示联通运营商的路由器
  • oracle tcp空包请求,再谈 TCP 的 CLOSE_WAIT

    背景 某日集群告警 hbase regionserver 因 fd 不足导致进程主动退出 简单排查后发现regionserver 到 datanode 的TCP 连接存在大量 CLOSE WAIT 单机总数有10万之多 众所周知 CLOSE
  • HTML表格

    目录 实例 表格 表格和边框属性 表格的表头 表格中的空单元格 更多实例 表格标签 一个完整的实例 本例涉及到的资源 eg background jpg eg cute gif 可以使用 HTML 创建表格 实例 表格 这个例子演示如何在
  • 一台电脑上安装两个Tomcat服务器

    在排查问题来源的时候 由于不想卸载之前下载的Tomcat 需要再安装一个Tomcat服务器 下载压缩版的Tomcat之后 第一个Tomcat配置不变 需要修改第二个Tomcat的配置 1 CATALINA HOME 8081 新的地址 2
  • flex:1可以撑满剩余空间

    flex 1 的妙用 首先 flex 是 flex grow flex shrink flex basis的缩写 当 flex 取值为一个非负数字 则该数字为 flex grow 值 flex shrink 取 1 flex basis 取
  • MySql中left join、right join、inner join实例分析,union与union all的区别,Mybatis中CDATA []的用法

    inner join select from user a inner join grade b on a gid b id 只返回两个表中联结字段相等的行 left join select from user a left join gr
  • bokeh python_Python Bokeh数据可视化教程

    bokeh python Bokeh is an interactive Python data visualization library which targets modern web browsers for presentatio