[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如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

基于Python打造一个智能单词管理神器

《基于Python打造一个智能单词管理神器》这篇文章主要为大家详细介绍了如何使用Python打造一个智能单词管理神器,从查询到导出的一站式解决,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 项目概述:为什么需要这个工具2. 环境搭建与快速入门2.1 环境要求2.2 首次运行配置3. 核心功能使用指

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义