【小沐学Python】在线web数据可视化Python库:Bokeh

文章目录

  • 1、简介
  • 2、安装
  • 3、测试
    • 3.1 创建折线图
    • 3.2 添加和自定义渲染器
    • 3.3 添加图例、文本和批注
    • 3.4 自定义您的绘图
    • 3.5 矢量化字形属性
    • 3.6 合并绘图
    • 3.7 显示和导出
    • 3.8 提供和筛选数据
    • 3.9 使用小部件
    • 3.10 嵌入Bokeh图表到Flask应用程序
  • 结语

1、简介

https://bokeh.org/
https://github.com/bokeh/bokeh

Bokeh是一个Python库,用于创建交互式的、现代化的Web可视化工具。它允许用户创建各种类型的图表,包括线图、散点图、柱状图、热图等,而且这些图表都可以在Web浏览器中交互式地操作。
在这里插入图片描述
Bokeh 是用于现代 Web 浏览器的交互式可视化库。它提供了优雅、简洁的多功能图形结构,并在大型或流数据集之间提供高性能交互性。Bokeh 可以帮助任何想要快速轻松地创建交互式绘图、仪表板和数据应用程序的人。

在这里插入图片描述

  • 交互性:Bokeh提供了丰富的交互性选项,使用户能够在图表上进行缩放、平移、选择数据点等操作。

  • 现代化的外观:Bokeh的图表外观非常现代化和吸引人,可以定制颜色、线条样式等。

  • 多种输出格式:Bokeh支持多种输出格式,包括HTML、Jupyter Notebook、交互式应用程序等。

  • 无需前端开发经验:使用Bokeh,不需要具备前端开发的经验,就可以创建交互式的Web可视化。

  • 支持大数据集:Bokeh能够有效地处理大数据集,因此适用于各种规模的数据分析任务。

Bokeh是一个用于数据可视化的强大工具,它能够创建交互式、高性能且具有各种可视化样式的图表。Bokeh提供了Python、R、Scala和Julia等多个编程语言的接口,使得用户可以根据自己的喜好选择使用。

在将Bokeh绘图嵌入到网站中之前,我们需要将绘图文件上传到网站的服务器。一种常见的方法是将绘图作为静态资源存储在服务器上,并在网站的HTML代码中引用它。

2、安装

请在 Bash 或 Windows 命令提示符下输入以下pip命令:

pip install bokeh

在这里插入图片描述

3、测试

3.1 创建折线图

Bokeh允许您使用Python构建在web浏览器中运行的交互式可视化。为了实现这一点,Bokeh包含了一个名为BokehJS的JavaScript库。BokehJS负责在浏览器中呈现可视化效果。
当您在Python中使用Bokeh创建可视化时,Bokeh会将此可视化转换为JSON文件。然后,这个JSON文件被发送到BokehJS,BokehJS在浏览器中呈现可视化效果。

在这里插入图片描述
在Python中使用Bokeh,它会自动生成BokehJS代码。但是,您也可以在JavaScript中直接使用BokehJS。
在这里插入图片描述

from bokeh.plotting import figure, show

# generate some values
x = list(range(1, 50))
y = [pow(x, 2) for x in x]

# create a new plot
p = figure()
# add a line renderer and legend to the plot
p.line(x, y, legend_label="Temp.")

# show the results
show(p)

在这里插入图片描述

from bokeh.plotting import figure, show

# prepare some data
x = [1, 2, 3, 4, 5]
y1 = [6, 7, 2, 4, 5]
y2 = [2, 3, 4, 5, 6]
y3 = [4, 5, 5, 7, 2]

# create a new plot with a title and axis labels
p = figure(title="Multiple line example", x_axis_label="x", y_axis_label="y")

# add multiple renderers
p.line(x, y1, legend_label="Temp.", color="blue", line_width=2)
p.line(x, y2, legend_label="Rate", color="red", line_width=2)
p.line(x, y3, legend_label="Objects", color="green", line_width=2)

# show the results
show(p)

在这里插入图片描述

3.2 添加和自定义渲染器

在本节中,您将使用不同的渲染器函数来创建各种 其他类型的图形。您还将自定义字形的外观。

from bokeh.plotting import figure, show

# prepare some data
x = [1, 2, 3, 4, 5]
y1 = [6, 7, 2, 4, 5]
y2 = [2, 3, 4, 5, 6]
y3 = [4, 5, 5, 7, 2]

# create a new plot with a title and axis labels
p = figure(title="Multiple glyphs example", x_axis_label="x", y_axis_label="y")

# add multiple renderers
p.line(x, y1, legend_label="Temp.", color="#004488", line_width=3)
p.line(x, y2, legend_label="Rate", color="#906c18", line_width=3)
p.scatter(x, y3, legend_label="Objects", color="#bb5566", size=16)

# show the results
show(p)

在这里插入图片描述

3.3 添加图例、文本和批注

from bokeh.plotting import figure, show

# prepare some data
x = [1, 2, 3, 4, 5]
y1 = [4, 5, 5, 7, 2]
y2 = [2, 3, 4, 5, 6]

# create a new plot
p = figure(title="Legend example")

# add circle renderer with legend_label arguments
line = p.line(x, y1, legend_label="Temp.", line_color="blue", line_width=2)
circle = p.scatter(
    x,
    y2,
    marker="circle",
    size=80,
    legend_label="Objects",
    fill_color="red",
    fill_alpha=0.5,
    line_color="blue",
)

# display legend in top left corner (default is top right corner)
p.legend.location = "top_left"

# add a title to your legend
p.legend.title = "Obervations"

# change appearance of legend text
p.legend.label_text_font = "times"
p.legend.label_text_font_style = "italic"
p.legend.label_text_color = "navy"

# change border and background of legend
p.legend.border_line_width = 3
p.legend.border_line_color = "navy"
p.legend.border_line_alpha = 0.8
p.legend.background_fill_color = "navy"
p.legend.background_fill_alpha = 0.2

# show the results
show(p)

在这里插入图片描述

3.4 自定义您的绘图

from bokeh.io import curdoc
from bokeh.plotting import figure, show

# prepare some data
x = [1, 2, 3, 4, 5]
y = [4, 5, 5, 7, 2]

# apply theme to current document
curdoc().theme = "dark_minimal"

# create a plot
p = figure(sizing_mode="stretch_width", max_width=500, height=250)

# add a renderer
p.line(x, y)

# show the results
show(p)

在这里插入图片描述

3.5 矢量化字形属性

生成了 不同的字形并定义了它们的外观。

import random

from bokeh.plotting import figure, show

# generate some data (1-10 for x, random values for y)
x = list(range(0, 26))
y = random.sample(range(0, 100), 26)

# generate list of rgb hex colors in relation to y
colors = [f"#{255:02x}{int((value * 255) / 100):02x}{255:02x}" for value in y]

# create new plot
p = figure(
    title="Vectorized colors example",
    sizing_mode="stretch_width",
    max_width=500,
    height=250,
)

# add line and scatter renderers
p.line(x, y, line_color="blue", line_width=1)
p.scatter(x, y, fill_color=colors, line_color="blue", size=15)

# show the results
show(p)

在这里插入图片描述

3.6 合并绘图

把多个地块组合成不同类型的布局。

from bokeh.layouts import row
from bokeh.plotting import figure, show

# prepare some data
x = list(range(11))
y0 = x
y1 = [10 - i for i in x]
y2 = [abs(i - 5) for i in x]

# create three plots with one renderer each
s1 = figure(width=250, height=250, background_fill_color="#fafafa")
s1.scatter(x, y0, marker="circle", size=12, color="#53777a", alpha=0.8)

s2 = figure(width=250, height=250, background_fill_color="#fafafa")
s2.scatter(x, y1, marker="triangle", size=12, color="#c02942", alpha=0.8)

s3 = figure(width=250, height=250, background_fill_color="#fafafa")
s3.scatter(x, y2, marker="square", size=12, color="#d95b43", alpha=0.8)

# put the results in a row and show
show(row(s1, s2, s3))

在这里插入图片描述

3.7 显示和导出

创建了 自定义和组合的可视化效果。

from bokeh.plotting import figure, output_file, save

# prepare some data
x = [1, 2, 3, 4, 5]
y = [4, 5, 5, 7, 2]

# set output to static HTML file
output_file(filename="custom_filename.html", title="Static HTML file")

# create a new plot with a specific size
p = figure(sizing_mode="stretch_width", max_width=500, height=250)

# add a scatter renderer
p.scatter(x, y, fill_color="red", size=15)

# save the results to a file
save(p)

在这里插入图片描述

3.8 提供和筛选数据

使用了不同的 用于显示和导出可视化效果的方法。

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

# create dict as basis for ColumnDataSource
data = {'x_values': [1, 2, 3, 4, 5],
        'y_values': [6, 7, 2, 3, 6]}

# create ColumnDataSource based on dict
source = ColumnDataSource(data=data)

# create a plot and renderer with ColumnDataSource data
p = figure(height=250)
p.scatter(x='x_values', y='y_values', size=20, source=source)
show(p)

在这里插入图片描述

from bokeh.layouts import gridplot
from bokeh.models import CDSView, ColumnDataSource, IndexFilter
from bokeh.plotting import figure, show

# create ColumnDataSource from a dict
source = ColumnDataSource(data=dict(x=[1, 2, 3, 4, 5], y=[1, 2, 3, 4, 5]))

# create a view using an IndexFilter with the index positions [0, 2, 4]
view = CDSView(filter=IndexFilter([0, 2, 4]))

# setup tools
tools = ["box_select", "hover", "reset"]

# create a first plot with all data in the ColumnDataSource
p = figure(height=300, width=300, tools=tools)
p.scatter(x="x", y="y", size=10, hover_color="red", source=source)

# create a second plot with a subset of ColumnDataSource, based on view
p_filtered = figure(height=300, width=300, tools=tools)
p_filtered.scatter(x="x", y="y", size=10, hover_color="red", source=source, view=view)

# show both plots next to each other in a gridplot layout
show(gridplot([[p, p_filtered]]))

在这里插入图片描述

3.9 使用小部件

from bokeh.layouts import layout
from bokeh.models import Div, RangeSlider, Spinner
from bokeh.plotting import figure, show

# prepare some data
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [4, 5, 5, 7, 2, 6, 4, 9, 1, 3]

# create plot with circle glyphs
p = figure(x_range=(1, 9), width=500, height=250)
points = p.scatter(x=x, y=y, size=30, fill_color="#21a7df")

# set up textarea (div)
div = Div(
    text="""
          <p>Select the circle's size using this control element:</p>
          """,
    width=200,
    height=30,
)

# set up spinner
spinner = Spinner(
    title="Circle size",
    low=0,
    high=60,
    step=5,
    value=points.glyph.size,
    width=200,
)
spinner.js_link("value", points.glyph, "size")

# set up RangeSlider
range_slider = RangeSlider(
    title="Adjust x-axis range",
    start=0,
    end=10,
    step=1,
    value=(p.x_range.start, p.x_range.end),
)
range_slider.js_link("value", p.x_range, "start", attr_selector=0)
range_slider.js_link("value", p.x_range, "end", attr_selector=1)

# create layout
layout = layout(
    [
        [div, spinner],
        [range_slider],
        [p],
    ],
)

# show result
show(layout)

在这里插入图片描述

3.10 嵌入Bokeh图表到Flask应用程序

要在Flask应用中嵌入一个Bokeh应用,我们需要进行以下步骤:

  • 安装Bokeh和Flask库。
  • 创建一个Flask应用。
  • 在Flask应用中创建一个Bokeh绘图函数。
  • 创建一个包含Bokeh绘图函数的HTML模板。
  • 在Flask应用中创建一个路由,渲染HTML模板并运行Bokeh绘图函数。

app.py代码如下:

from flask import Flask, render_template
from bokeh.plotting import figure
from bokeh.embed import components
 
app = Flask(__name__)
 
@app.route('/')
def index():
    # 创建一个图表对象
    p = figure(title="Embedded Bokeh Plot")
    
    # 添加数据点
    x = [1, 2, 3, 4, 5]
    y = [6, 7, 2, 4, 5]
    
    # 绘制折线
    p.line(x, y, line_width=2)
    
    # 将图表组件嵌入到HTML模板中
    script, div = components(p)
    
    return render_template('index.html', script=script, div=div)

if __name__ == '__main__':
    app.run()

index.html代码如下:

<!DOCTYPE html>
<html>
<head>
    <title>Bokeh Plot</title>
    <link href="https://cdn.bokeh.org/bokeh/release/bokeh-3.4.2.min.css" rel="stylesheet" type="text/css">
    <script src="https://cdn.bokeh.org/bokeh/release/bokeh-3.4.2.min.js"></script>
</head>
<body>
    <h1>Bokeh Plot</h1>
    <div>
        {{ script | safe }}
        {{ div | safe }}
    </div>
</body>
</html>

执行命令如下:

python app.py

在这里插入图片描述

结语

如果您觉得该方法或代码有一点点用处,可以给作者点个赞,或打赏杯咖啡;╮( ̄▽ ̄)╭
如果您感觉方法或代码不咋地//(ㄒoㄒ)//,就在评论处留言,作者继续改进;o_O???
如果您需要相关功能的代码定制化开发,可以留言私信作者;(✿◡‿◡)
感谢各位大佬童鞋们的支持!( ´ ▽´ )ノ ( ´ ▽´)っ!!!

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

从0到1:培训老师预约小程序开发笔记二

背景调研 培训老师预约小程序&#xff1a; 教师和学生可以更便捷地安排课程&#xff0c;并提升教学质量和学习效果&#xff0c;使之成为管理和提升教学效果的强大工具。培训老师可以在小程序上设置自己的可预约时间&#xff0c;学员可以根据老师的日程安排选择合适的时间进行预…

【HICE】dns正向解析

1.编辑仓库 2.挂载 3.下载软件包 4.编辑named.conf 5.编辑named.haha 6.重启服务 7.验证本地域名是否解析

Mysql 数据库主从复制-CSDN

查询两台虚拟机的IP 主虚拟机IP 从虚拟机IP服务 修改对应的配置文件 查询对应配置文件的命令 find / -name my.cnf编辑对应的配置文件 主 my.cnf &#xff08;部分配置&#xff09; [mysqld] ########basic settings######## server_id 1 log_bin /var/log/mysql/mysql-…

leetcode 709. 转换成小写字母

leetcode 709. 转换成小写字母 题解 class Solution { public:string toLowerCase(string s) {string ans;for (int i 0; i < s.size(); i) {if (s[i] > A && s[i] < Z) {s[i] 32;}ans s[i];}return ans;} };

室内定位可视化:精准导航与实时位置展示

通过图扑室内定位可视化技术&#xff0c;提供精准的导航服务和实时位置展示&#xff0c;帮助用户高效找到目标地点&#xff0c;提升空间管理和资源配置的效率与体验。

【昇思25天学习打卡营打卡指南-第十九天】基于MobileNetv2的垃圾分类

CycleGAN图像风格迁移互换 模型介绍 模型简介 CycleGAN(Cycle Generative Adversarial Network) 即循环对抗生成网络&#xff0c;来自论文 Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks 。该模型实现了一种在没有配对示例的情况下学习…

go语言的异常处理机制

error 在go语言中&#xff0c;异常被定义为实现了error接口的类型&#xff0c;error接口只定义了一个返回string类型Error&#xff08;&#xff09;方法&#xff0c;任何实现了Error()方法的类型都可以被定义为异常&#xff0c;以下是一个自定义的异常类型&#xff1a; typ…

【零基础】学JS

喝下这碗鸡汤 “知识就是力量。” - 弗朗西斯培根 1.三元运算符 目标:能利用三元运算符执行满足条件的语句 使用场景:其实是比if双分支更简单的写法&#xff0c;可以使用三元表达式 语法&#xff1a;条件 ? 满足条件的执行代码 : 不满足条件执行的代码 接下来用一个小案例来展…

昇思25天学习打卡营第十四天|Pix2Pix实现图像转换

训练营进入第十四天&#xff0c;今天学的内容是Pix2Pix图像转换&#xff0c;记录一下学习内容&#xff1a; Pix2Pix概述 Pix2Pix是基于条件生成对抗网络&#xff08;cGAN, Condition Generative Adversarial Networks &#xff09;实现的一种深度学习图像转换模型&#xff0c…

读书笔记-《魔鬼经济学》

这是一本非常有意思的经济学启蒙书&#xff0c;作者探讨了许多问题&#xff0c;并通过数据找到答案。 我们先来看看作者眼中的“魔鬼经济学”是什么&#xff0c;再选一个贴近我们生活的例子进行阐述。 01 魔鬼经济学 中心思想&#xff1a;假如道德代表人类对世界运转方式的期…

Vue 3集成krpano 全景图展示

Vue 3集成krpano 全景图展示 星光云全景系统源码 VR全景体验地址 星光云全景VR系统 将全景krpano静态资源文件vtour放入vue项目中 导入vue之前需要自己制作一个全景图 需要借助官方工具进行制作 工具下载地址&#xff1a;krpano工具下载地址 注意事项&#xff1a;vuecli…

LRU缓存算法设计

LRU 缓存算法的核⼼数据结构就是哈希链表&#xff0c;双向链表和哈希表的结合体。这个数据结构⻓这样&#xff1a; 创建的需要有两个方法&#xff0c;一个是get方法&#xff0c;一个是put方法。 一些问题&#xff1a;为什么需要使用双向链表呢&#xff1f;因为删除链表的本身&…

2-26 基于matlab开发的制冷循环模型

基于matlab开发的制冷循环模型。Simscape两相流域中的制冷循环模型&#xff0c;在simulink中完成多循环温度控制。程序已调通&#xff0c;可直接运行。 2-26 制冷循环模型 Simscape两相流域 - 小红书 (xiaohongshu.com)

Web3D引擎,three.js堪称扛把子,Babylon.js差点意思。

涉及到Web3D开发&#xff0c;Three.js和Babylon.js是两个备受推崇的引擎。它们都是基于WebGL的开源3D引擎&#xff0c;用于创建交互式的3D图形应用程序&#xff0c;但要细论起来&#xff0c;three.js普及度远超Babylon .js. 一、二者的介绍 Three.js&#xff1a; Three.js 是一…

Android仿今日头条新闻(一)

新建一个侧边栏的文件&#xff0c;创建成功后直接运行。可以看到带滑动的侧边栏功能如图所示&#xff1a; 主体UI&#xff1a; 新闻UI的实现: 侧边栏&#xff1a; 更换一下颜色&#xff1a; 学习参考-浩宇开发

Objects365数据集介绍

Objects365数据集介绍 什么是Objects365数据集&#xff1f;数据集的规模与内容数据集的特点数据集下载 什么是Objects365数据集&#xff1f; Objects365是一个大规模、高质量的物体检测数据集。该数据集旨在推动物体检测技术的发展&#xff0c;特别是在真实世界场景下的应用。O…

STM32-01 推挽输出-点亮LED

本文以STM32中点亮LED为例&#xff0c;解读推挽输出的原理 推挽输出介绍 所谓的推挽输出&#xff0c;就是通过控制输出控制模块&#xff0c;打开或者关闭P-MOS或者N-MOS。 ─ 推挽模式下&#xff1a;输出寄存器上的’0’激活N-MOS&#xff0c;而输出寄存器上的’1’将激活P-M…

尚品汇-(十三)

&#xff08;1&#xff09;查询sku列表 在ManageService 中添加 /*** SKU分页列表* param pageParam* return*/ IPage<SkuInfo> getPage(Page<SkuInfo> pageParam);接口实现类 Override public IPage<SkuInfo> getPage(Page<SkuInfo> pageParam) {Qu…

【双一流高校主办,Springer-LNICST出版,EI稳定检索】2024年应用计算智能、信息学与大数据国际会议(ACIIBD 2024,7月26-28)

2024年应用计算智能、信息学与大数据国际学术会议&#xff08;ACIIBD 2024&#xff09;将于2024年7月26-28日在中国广州举办。会议将聚焦于计算智能及其应用、信息、大数据等相关的研究领域&#xff0c; 广泛邀请国内外知名专家学者&#xff0c;共同探讨相关学科领域的最新发展…

Ubuntu + SSH密钥连接服务器

1. 下载VS code cd到下载文件夹后&#xff0c;使用命令安装&#xff0c;把xxx复制为文件名 sudo dpkg -i xxx.deb2. 为VSCode换皮肤 3. 下载SSH插件和Docker插件 4. 配置SSH 把密钥key文件放在/home/your_user_name/.ssh/里面&#xff0c;然后在/home/your_user_name/.ssh/c…