[python]bokeh学习总结——bokeh.layouts

2024-03-15 15:08

本文主要是介绍[python]bokeh学习总结——bokeh.layouts,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

如果希望在同一张图上显示多个图像,可以使用bokeh.layouts类中的方法:

  • row()
  • column()
  • gridplot()
  • widgetbox()
  • layout()

row()

row()的作用是将多个图像以行的方式放到同一张图中。

from bokeh.io import output_file, show
from bokeh.layouts import row
from bokeh.plotting import figureoutput_file("layout.html")x = list(range(11))
y0 = x
y1 = [10 - i for i in x]
y2 = [abs(i - 5) for i in x]# create a new plot
s1 = figure(plot_width=250, plot_height=250, title=None)
s1.circle(x, y0, size=10, color="navy", alpha=0.5)# create another one
s2 = figure(plot_width=250, plot_height=250, title=None)
s2.triangle(x, y1, size=10, color="firebrick", alpha=0.5)# create and another
s3 = figure(plot_width=250, plot_height=250, title=None)
s3.square(x, y2, size=10, color="olive", alpha=0.5)# put the results in a row
show(row(s1, s2, s3))

column()

column()的作用是将多个图像以列的方式放到同一张图像中。

from bokeh.io import output_file, show
from bokeh.layouts import column
from bokeh.plotting import figureoutput_file("layout.html")x = list(range(11))
y0 = x
y1 = [10 - i for i in x]
y2 = [abs(i - 5) for i in x]# create a new plot
s1 = figure(plot_width=250, plot_height=250, title=None)
s1.circle(x, y0, size=10, color="navy", alpha=0.5)# create another one
s2 = figure(plot_width=250, plot_height=250, title=None)
s2.triangle(x, y1, size=10, color="firebrick", alpha=0.5)# create and another
s3 = figure(plot_width=250, plot_height=250, title=None)
s3.square(x, y2, size=10, color="olive", alpha=0.5)# put the results in a column and show
show(column(s1, s2, s3))

gridplot()

如果希望以矩阵的方式将多个图像放到同一张图上,可以使用gridplot()函数,如果某个位置的图像为空,可以设定为None。

from bokeh.io import output_file, show
from bokeh.layouts import gridplot
from bokeh.palettes import Viridis3
from bokeh.plotting import figureoutput_file("layout_grid.html")x = list(range(11))
y0 = x
y1 = [10 - i for i in x]
y2 = [abs(i - 5) for i in x]# create three plots
p1 = figure(plot_width=250, plot_height=250, title=None)
p1.circle(x, y0, size=10, color=Viridis3[0])
p2 = figure(plot_width=250, plot_height=250, title=None)
p2.triangle(x, y1, size=10, color=Viridis3[1])
p3 = figure(plot_width=250, plot_height=250, title=None)
p3.square(x, y2, size=10, color=Viridis3[2])# make a grid
grid = gridplot([[p1, p2], [None, p3]])# show the results
show(grid)

要注意的是,gridplot()函数只能接受figure类,不能接受widget类,如果希望一张图像中既有figure类也有widget类,就需要用layout()方法。文档中关于gridplot()的介绍如下,可以看到输入参数只能是Plot类,即figure类:


widgetbox()

如果希望在图像中插入widgets(小部件),可以使用widgetbox()函数。

from bokeh.io import output_file, show
from bokeh.layouts import widgetbox
from bokeh.models.widgets import Button, RadioButtonGroup, Select, Slideroutput_file("layout_widgets.html")# create some widgets
slider = Slider(start=0, end=10, value=1, step=.1, title="Slider")
button_group = RadioButtonGroup(labels=["Option 1", "Option 2", "Option 3"], active=0)
select = Select(title="Option:", value="foo", options=["foo", "bar", "baz", "quux"])
button_1 = Button(label="Button 1")
button_2 = Button(label="Button 2")# put the results in a row
show(widgetbox(button_1, slider, button_group, select, button_2, width=300))

layout()

layout()函数的作用有两个:

  1. 将多个图像按照矩阵样式摆放到同一张图中
  2. 输入参数可以是Plot, Widget, WidgetBox, Row, Column, ToolbarBox, Spacer,不仅限于Plot类

官网文档中的介绍如下:


来自github中的一个例子如下:

import numpy as npfrom bokeh.layouts import layout
from bokeh.models import CustomJS, Slider, ColumnDataSource, WidgetBox
from bokeh.plotting import figure, output_file, showoutput_file('dashboard.html')tools = 'pan'def bollinger():# Define Bollinger Bands.upperband = np.random.random_integers(100, 150, size=100)lowerband = upperband - 100x_data = np.arange(1, 101)# Bollinger shading glyph:band_x = np.append(x_data, x_data[::-1])band_y = np.append(lowerband, upperband[::-1])p = figure(x_axis_type='datetime', tools=tools)p.patch(band_x, band_y, color='#7570B3', fill_alpha=0.2)p.title.text = 'Bollinger Bands'p.title_location = 'left'p.title.align = 'left'p.plot_height = 600p.plot_width = 800p.grid.grid_line_alpha = 0.4return [p]def slider():x = np.linspace(0, 10, 100)y = np.sin(x)source = ColumnDataSource(data=dict(x=x, y=y))plot = figure(y_range=(-10, 10), tools='', toolbar_location=None,title="Sliders example")plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)callback = CustomJS(args=dict(source=source), code="""var data = source.data;var A = amp.value;var k = freq.value;var phi = phase.value;var B = offset.value;var x = data['x']var y = data['y']for (var i = 0; i < x.length; i++) {y[i] = B + A*Math.sin(k*x[i]+phi);}source.change.emit();""")amp_slider = Slider(start=0.1, end=10, value=1, step=.1, title="Amplitude", callback=callback, callback_policy='mouseup')callback.args["amp"] = amp_sliderfreq_slider = Slider(start=0.1, end=10, value=1, step=.1, title="Frequency", callback=callback)callback.args["freq"] = freq_sliderphase_slider = Slider(start=0, end=6.4, value=0, step=.1, title="Phase", callback=callback)callback.args["phase"] = phase_slideroffset_slider = Slider(start=-5, end=5, value=0, step=.1, title="Offset", callback=callback)callback.args["offset"] = offset_sliderwidgets = WidgetBox(amp_slider, freq_slider, phase_slider, offset_slider)return [widgets, plot]def linked_panning():N = 100x = np.linspace(0, 4 * np.pi, N)y1 = np.sin(x)y2 = np.cos(x)y3 = np.sin(x) + np.cos(x)s1 = figure(tools=tools)s1.circle(x, y1, color="navy", size=8, alpha=0.5)s2 = figure(tools=tools, x_range=s1.x_range, y_range=s1.y_range)s2.circle(x, y2, color="firebrick", size=8, alpha=0.5)s3 = figure(tools='pan, box_select', x_range=s1.x_range)s3.circle(x, y3, color="olive", size=8, alpha=0.5)return [s1, s2, s3]l = layout([bollinger(),slider(),linked_panning(),
], sizing_mode='stretch_both')show(l)


这篇关于[python]bokeh学习总结——bokeh.layouts的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/812373

相关文章

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

Python办公自动化实战之打造智能邮件发送工具

《Python办公自动化实战之打造智能邮件发送工具》在数字化办公场景中,邮件自动化是提升工作效率的关键技能,本文将演示如何使用Python的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、

Python包管理工具pip的升级指南

《Python包管理工具pip的升级指南》本文全面探讨Python包管理工具pip的升级策略,从基础升级方法到高级技巧,涵盖不同操作系统环境下的最佳实践,我们将深入分析pip的工作原理,介绍多种升级方... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核