[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中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

Python设置Cookie永不超时的详细指南

《Python设置Cookie永不超时的详细指南》Cookie是一种存储在用户浏览器中的小型数据片段,用于记录用户的登录状态、偏好设置等信息,下面小编就来和大家详细讲讲Python如何设置Cookie... 目录一、Cookie的作用与重要性二、Cookie过期的原因三、实现Cookie永不超时的方法(一)

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

JavaSE正则表达式用法总结大全

《JavaSE正则表达式用法总结大全》正则表达式就是由一些特定的字符组成,代表的是一个规则,:本文主要介绍JavaSE正则表达式用法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录常用的正则表达式匹配符正则表China编程达式常用的类Pattern类Matcher类PatternSynta

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提