在 Bokeh 中构建图表

发布日期:2026-07-10 00:27:03   来源 : 杭州电子商务研究院    浏览量 :8
杭州电子商务研究院 发布日期:2026-07-10 00:27:03  
8

介绍

Bokeh 是一个 Python 库,它使我们能够轻松快速地构建交互式图表、图表、仪表板和数据应用程序。这就是为什么现在它比 Maplotlib 和 Seaborn 等同类库使用得更频繁的原因。在本指南中,我们将学习如何使用 Bokeh 应用不同的方法以交互和动态的方式可视化数据。

什么是散景?

Bokeh 是一个新推出的 Python 库,类似于 D3.js,用于针对 Web 浏览器的交互式数据可视化。它能够在大型和流式数据集上精确而优雅地构建具有高交互性和高性能的多功能图形。

散景的优点

  • Bokeh 使用 JavaScript 来呈现可视化图形,这使其成为构建基于 Web 的仪表板和应用程序的绝佳选择,与 Seaborn、Matplotlib 和 ggplot 不同。

  • Bokeh 可用于将可视化效果嵌入到 Django 和 Flask 应用程序中。

  • 使用 Bokeh,我们可以使用简单的命令行构建复杂的图表。

  • Bokeh 提供多种格式的输出,包括 HTML、笔记本和服务器。

安装 Bokeh

  • pip install:要使用pip安装,请打开终端并运行以下命令:
      pip install Bokeh
    
  • conda 安装:要使用conda安装,请打开终端并运行以下命令:
      conda install -c bokeh bokeh
    
  • 你可以通过运行以下命令来验证你的系统上是否安装了 Bokeh
      bokeh --version
    

如果命令成功运行,则表示 Bokeh 已安装在您的系统上。

Bokeh 的模块

Bokeh 提供了强大而灵活的数据可视化功能,可实现简单性和高度先进的数据定制。

  • Bokeh.models:这是一个低级模块,为开发人员提供了极大的灵活性。

  • Bokeh.plotting:这是一个用于散点图和简单创建可视化字形的高级模块。

注意:引入此库时有三个模块,包括Bokeh.Charts。这被视为高级模块,而Bokeh.Plotting被视为中级模块。Bokeh.charts很久以前就被弃用了,随后被删除

使用 Bokeh 创建图表或绘图

首先,我们将了解如何使用不同的字形绘制线图、散点图、多线图、彩色图和网格图。我们还将了解 Bokeh 中使用的字形和不同类型的标记的各种属性。最后,我们将使用 Numpy 和 Pandas 使用数据框绘制图形。

先决条件

  • 我假设您对 Python 及其 Pandas 和 Numpy 库有基本的了解。

  • 我假设您已经在系统上安装了 Python 3 并且有一个 Web 浏览器。

图 1:简单的线形符号

      from bokeh.plotting import figure, output_file, show

# creating a random data 
x= [1,2,6,8,5]
y= [4,5,2,5,3]

# output to a HTML file
output_file('index.html')

# create a new plot with a title and axis labels
p= figure(
    title='simple example', 
    x_axis_label= 'X Axis',
    y_axis_label= 'Y Axis'
)

# adding a legend and line renderer with width
p.line(x,y, legend='Graph', line_width = 4)

# show result 
show(p)
    

这将在您的目录中创建一个名为index.html的文件。在浏览器中打开该文件以查看以下结果(如果您使用 Jupyter Notebook 或 Spyder,它将自动在您的浏览器中打开):

情节 2:多线情节

      from bokeh.plotting import figure, output_file, show

output_file("patch.html")
# x-y coordinate data
x = [1.5, 2.5, 1,3]
y = [2, 4, 6,3.8]

# x-y coordinate data
x1 = [1.5, 2.5, 1,3]
y1 = [1, 1.8, 2.5,3.8]
p = figure(plot_width=600, plot_height=600)

p.multi_line(
    [x, y], [x1, y1],
    color=["Red", "navy"], 
    alpha=[0.8, 0.3],
    line_width=4
)
#show result
show(p)
    

在上图中,您可以看到顶部的工具(缩放、调整大小、重置、滚轮缩放)。这些工具允许您与图表进行交互。我们还将在下一个示例中学习如何删除它们。

图 3:没有工具栏的简单散点符号

      # Import Bokeh Libraries
from bokeh.plotting import figure, output_file, show

# x-y coordinate data
x = [1.5, 2.5, 1,3]
y = [1, 1.8, 2.5,3.8]

# Output File name.
output_file('scatter.html', title='Circle Glyphs')

# figure with no toolbar and axis ranges of [0,4]
p = figure(
	title='simple scatter plot',
	plot_height=500, plot_width=600,
	x_axis_label='X Axis', y_axis_label='Y Axis',
	x_range =(0,4), y_range = (0,4),
    toolbar_location=None
)

# Render Glyph
p.circle(x,y,color='Red', size=10)

# Show plot
show(p)
    

在上面的输出中,您可以看到没有显示任何工具。

使用Figure()函数,我们可以为 x 轴和 y 轴提供标题以及图例和图形名称。这有助于我们更详细地描述我们在图形上绘制的数据。

散景中的字形

在 Bokeh 中,形状的视觉属性称为字形。这包括以下形状的类型和属性:

  • 视觉形状

    • 圆形、三角形、正方形

    • 矩形线、楔形

  • 形状所附带的属性

    • 坐标(x,y)

    • 尺寸、颜色、透明度

图 4:改变字形的属性

      from bokeh.plotting import figure, output_file, show
output_file("size.html")

p = figure(plot_width = 600,plot_height =600)
# changing size of markers
# rendering glyph

p.circle(x=10, y=[3,5,6,15], size=[10,20,30,40])
#show result
show(p)
    

注意:您可以在 Bokeh 中使用不同的标记。其中包括:

  • 星号()
  • 圆圈()
  • circle_cross()
  • circle_x()
  • 三角形()
  • 叉()
  • 钻石()
  • 钻石十字()
  • square_x()
  • 倒三角形()
  • 正方形()
  • x()
  • square_cross()

图 5:线与标记一起

      # import bokeh library
from bokeh.plotting import figure, output_file, show
# coordinates data
x = [4,3,7,6,5]
y = [4,6,2,7,3]
p = figure()
#render line glypd
p.line(x, y, line_width=2,color='red')
#render cirlce glyphs
p.circle(x, y, fill_color='black', size=20)
output_file('line.html')
#show result
show(p)
    

字形类型

Bokeh 提供不同类型的字形。下面列出了一些我们可以在绘图中使用的字形。

  • 环()
  • 环形楔形()
  • 楔()
  • 矩形()
  • 四边形()
  • 变量栏()
  • hbar()
  • 图像()
  • 图像RGBA()
  • 图片网址()
  • 修补()
  • 补丁()
  • 线()
  • 多行()
  • 圆圈()
  • 椭圆形()
  • 椭圆()
  • 弧()
  • 二次函数()
  • 贝塞尔()

图 6:列表列表中给出的数据的多块字形

      from bokeh.plotting import figure, output_file, show 

#data in list of lists 
x = [ [3,3,1.5,1.5], [6,6,3.5], [2.5,15,4.5,7.5] ] 
y = [ [2,5,5,2], [3,5,5], [2,3,4,2] ] 

#add plot 
p = figure(title='multipacthglyph', 
                  plot_height=500, plot_width=600, 
                  x_axis_label='X Axis', y_axis_label='Y Axis') 

#render patch glyph 
p.patches(x, y,fill_color = ['red', 'blue','green'],line_color = 'white')
output_file('multipatches.html')
show(p)
    

借助 Numpy 和 Pandas 绘制图表

由于 Bokeh 的可定制性,它与 Python 中的数据结构库(如 Numpy 和 Pandas)兼容。Bokeh 也有自己的数据结构 columndatastructures 因此我们将研究如何在 Bokeh 中实现 Numpy、Pandas 和columndatastructure 。

绘图 7:借助 Numpy 绘制简单绘图

      from bokeh.plotting import figure, output_file, show
# importing numpy library
import numpy as np
# creating random data using n
以上内容来自杭州电子商务研究院推送
关注
关于我们
热门推荐
合作伙伴
免责声明:本站部分资讯来源于网络,如有侵权请及时联系客服,我们将尽快处理
Copyright © 2025-2027 ToB产业网址导航 公安备案 浙公网安备33010602013138号 浙ICP备16025413号-9
支持 反馈 关注 数据