散景速查表

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

介绍

Bokeh 是一个新推出的 Python 库,与 D3.js 类似,用于针对 Web 浏览器的交互式数据可视化。Bokeh 与其他 Python 可视化库(如 Matplotlib 或 Seaborn)的区别在于,它能够精确而优雅地构建多功能图形,在大型和流数据集中具有高交互性和高性能。Bokeh 还可用于将可视化嵌入到 Django 和 Flask 中。

在这份备忘单中,我们将学习在 Bokeh 的高级模块bokeh.plotting的帮助下创建图表的基础知识。

使用 Bokeh.plotting 创建绘图的步骤

以下是创建情节的步骤。我们稍后将详细讨论每个步骤。

  • 导入库及其 API

  • 准备数据

  • 指定文件的保存方式和位置

  • 通过可视化定制创建新图表并添加数据渲染器

  • 显示或保存结果

以下是 Bokeh 中的一个简单示例:

      # Step1 = importing the library
from bokeh.plotting import figure, output_file, show

# Step2= preparing the data 
x = [1,5,3,4]
y = [1,2,3,5]

# Step3 = Specify name and location 
output_file('index.html')

# Step4 = create a new 
p = figure(plot_height=250, plot_width=300)

# adding a legend and line renderer with width
p.line(x,y, color = 'Red')

# Step 5 = show result 
show(p)
    

导入库

我们正在研究如何使用bokeh.plotting bokeh 库的高级模块)进行绘图。bokeh.plotting主要以figure()类为中心,因此我们将研究如何导入该模块。

      from bokeh.plotting import figure
    

如果我们想使用Columndatasource ,则导入 API :

      from bokeh.models import ColumnDataSource
    

导入 bokeh.io 如果我们想指定如何以及在何处保存文件:

      from bokeh.io import output_notebook, show
    

我们还可以同时导入图形output_notebook

      from bokeh.plotting import figure, output_file, show
    

对于gridplot,我们必须从bokeh.layouts导入gridplot函数

      from bokeh.layouts import gridplot
    

数据准备

  • 在 Bokeh 中,我们可以直接提供数据,如本例所示:
      from bokeh.plotting import figure
x_values = [4, 2, 7, 4, 8]
y_values = [4, 5, 2, 6, 9]

p = figure()
p.circle(x=x_values, y=y_values)
    

我们直接传递的数据或者通过Pandas传递的数据都会被转换成columndatasource,但是我们也可以直接传递它。

      from bokeh.plotting import figure
from bokeh.models import ColumnDataSource

# passing data in columndatasource
data = {'x_values': [4, 2, 7, 4, 8],
   'y_values': [2, 6, 9, 3, 6]}
source = ColumnDataSource(data=data)

p = figure()
p.circle(x = 'x_values', y = 'y_values', source = source)
    

指定位置

我们可以使用bokeh.io API 来命名或保存我们的输出文件。建议在添加图并将其渲染到图之前指定或命名文件。

输出文件()

调用 output_file()来命名文件或在调用show()save()函数时生成默认输出状态。

      # importing the output_file() function
from bokeh.io import output_file
output_file("name.html")
    

导出png()

export_png()用于将文件导出为 .png 文件类型。此函数用法类似于save()show函数。

      # importing the export_png() function
from bokeh.io import export_png

# exporting the resulted file as png
export_png(plot, filename = "plot.png")
    

导出_svgs()

export_svgs() is used to export the file as .svg file type. This function usage is also similar to save() and show functions .

      # importing the export_svgs() function
from bokeh.io import export_svgs

plot.output_backend = "svg"
# exporting the resulted file as svgs
export_svgs(plot, filename = "plot.svg")
    

push_notebook()

By passing the push_notebook() function, we can make Bokeh show the results of show() calls on the Jupyter output cell since the last call of push_notebook() .

      from bokeh.plotting import figure
from bokeh.io import output_notebook, push_notebook, show

output_notebook()

plot = figure(plot_height= 250, plot_width = 300)
plot.circle([1,2,3], [4,6,5], size = 10, color ='red')

handle = show(plot, notebook_handle=True)

# Update the plot title in the earlier cell
plot.title.text = "Test Title"
push_notebook(handle = handle)
    

In this screenshot, you can see the plot shown in the output cell of the Jupyter Notebook.

Creating Plots and Adding Renders

First, we have to create a new figure for plotting using figure() class. figure() objects have many glyph methods that can be used to draw different types of plots :

  • annulus()
  • annular_wedge(), wedge()
  • rect(), quad()
  • image(), image_rgba(), image_url()
  • patch().patches()
  • line(), multi_line()
  • circle(), oval()
  • ellipse(), arc()
  • quadratic(), bezier()

Simple Line Glyph

      from bokeh.plotting import figure, output_file, show

#plot creation and adding renders 
p = figure(plot_width = 250, plot_height = 300)
p.line([1,2,3], [2,3,4], line_width = 4)
show(p)
    

Multi-line Glyph

      from bokeh.plotting import figure, output_file, show
x = [1,2,3,4]
y = [4,6,7,8]
x1 = [5,7,8,9]
y1 = [7,8,3,2]
# crearing a plot
p = figure()
# adding renders to the plot
p.multi_line([x, y], [x1, y1], color=["Red", "navy"], alpha=[0.8, 0.3], line_width=4)
show(p)
    

Circle Glyph

      from bokeh.plotting import figure, output_file, show

# plot creation and adding renders
plot = figure(plot_width=300, plot_height=300)
plot.circle(x=[1, 2, 3], y=[1, 2, 3], size=20)

show(plot)
    

Gridplot Layout

      import numpy as np
from bokeh.plotting import figure, output_file, show
# importing gridplot function 
from bokeh.layouts import gridplot

# prepare some data using numpy
x = np.linspace(0, 15, 80)
y = np.cos(x)
y1 = np.sin(x)
y2 = np.sin(x) + np.cos(x)

# outputfile
output_file("grid.html")

# Create a new plot
a1 = figure(width=250, plot_height=250, title=None)
a1.circle(x, y, size=10, color="yellow", alpha=0.5)

# New plot with same range
a2 = figure(width=250, height=250, x_range=a1.x_range, y_range=a1.y_range, title=None)
a2.triangle(x, y1, size=10, color="Grey", alpha=0.5)

# New plot with x axis same range
a3 = figure(width=250, height=250, x_range=a1.x_range, title=None)
a3.square(x, y2, size=10, color="green", alpha=0.5)

# NEW: put the subplots in a gridplot
p = gridplot([[a1, a2, a3]], toolbar_location=None)

#show result
show(p)
    

Wedge Layout

      from bokeh.plotting import figure, output_file, show
# Adjusting the plot width and height
plot = figure(plot_width=250, plot_height=300)
# Rendering the glyph 
plot.wedge(x = [4, 5, 6], y = [2, 3, 4], 
           radius = 17, start_angle=0.8, end_angle=3.1,
           radius_units="screen", color="green")
show(plot)
    

Scatter Plot

There is also a scatter function that can be parameterized by marker type .

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

# x-y coordinate data
x = [1.5, 2.5, 1]
y = [1, 1.8, 2.5]
#Specify fie name
output_file('scatter.html', title='Circle Glyphs')
#plot creation
p = figure(
  title='simple scatter plot',
  plot_height=250, plot_width=300
)
#using scatter function 
p.scatter(x,y, marker="square",size = 16, fill_color="red")
show(p)
    
<a target="_
以上内容来自杭州电子商务研究院推送
关注
关于我们
热门推荐
合作伙伴
免责声明:本站部分资讯来源于网络,如有侵权请及时联系客服,我们将尽快处理
Copyright © 2025-2027 ToB产业网址导航 公安备案 浙公网安备33010602013138号 浙ICP备16025413号-9
支持 反馈 关注 数据