Bokeh
文章目录
基本概念
Figure 对象
- 绘图的容器
- 其中可以存在多种图形处理工具 renderer(GlyPhRenderer)
- 处理器包含图形 glyph(Line)
- 图形处理器 renderer
glyph
- 各种形状本身的特性,不包括图例(legend)
- line 直线
- vbar 柱状图
每一个图形的属性
- x
- y
- line_color
- …
图形属性 glyph properties
- x
- y
- 颜色
- …
属性类型
可见属性 Visual Properties
- 参考:Customizing-Visual-Properties — Bokeh 2.4.3 Documentation
- 文本属性
- 填充属性
- 线的属性
风格属性
- 参考:Styling visual attributes — Bokeh 2.4.3 Documentation
Plot
- 大小
- 标题
- 背景
- 边界 border
- 轮廓 outline
区域大小问题
在 min_border 满足绘图需求时,有以下计算公式
width = min_border_left + frame_width + min_boder_right height = min_border_left + frame_height + min_boder_right
主绘图区域大小
figure() 函数中的 frame_width 和 frame_height,
他们不包括 title, axis, border pading 等
注意:
- frame_width, frame_height 和 width, height 不能同时使用
- 同时使用时, 以 frame_width 和 frame_height 为主
边界大小
axis_label, title, hovertool 等所占的区域
min_boder- min_border_left
- min_border_right
- min_border_top
- min_border_bottom
整个图(canvas)大小
figure() 函数中的 with 和 height
FAQ
如何隐藏(删除) 工具栏中的 bokeh 图标(logo)
参考:
- python - Remove bokeh logo from a list of figures - Stack Overflow
- python - Is it possible to remove specific tool from bokeh toolbar specially …
方法:
- 使用参数
toolbar_options=dict(logo=None)
例子:
| |
如何区域着色
参考:
特点:
- 类似 matplotlib.pyplot.fill 的多边形(polygon)区域着色
方法:
- 使用
Patch或Patchs
如何添加在任意位置添加文本注释标签
参考:
方法:
- 单个文本标签使用
Label工具 - 多个文本标签使用 LabelSet 工具
| |
如何使用 latex 公式
坐标轴设置
参考:
Ranges and axes — Bokeh 3.0.1 Documentation
- 搜索 x_range 就可以定位到相关文档
axes — Bokeh 2.4.0 Documentation
- Axis 类的设置
坐标轴范围
使用 figure 函数
figure(x_range=(1,100), y_range_name=(1,20))
使用 Figure 对象的属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15from bokeh.plotting import figure, show from bokeh.models import Range1d import numpy as np p = figure() x= np.arange(1, 10, 0.5) p.line(x, x**2) # 手动设置 p.x_range = Range1d(1, 10) p.y_range = Range1d(1, 100) show(p)
坐标轴名称
使用 figure 函数
figure(x_axis_label='X', y_axis_label='Y')
使用 Figure 对象属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15from bokeh.plotting import figure, show from bokeh.models import Range1d import numpy as np p = figure() x= np.arange(1, 10, 0.5) p.line(x, x**2) # 手动设置 p.xaxis.axis_label = 'X' p.yaxis.axis_label = 'Y' show(p)
第二坐标轴设置
| |
坐标轴标签 axis_label 设置
| |
着标轴 ticks 设置
| |
鼠标悬浮显示坐标
参考:
方法:
- 使用
HoverTool工具
举例:
| |
坐标显示设置
- 任意位置坐标,data_unit
tooltips=[("x,y", "$x, $y")]- 任意位置坐标,screen_unit
tooltips=[("x,y", "$sx, $sy")]- 绘图元素(Glyph)上的点
tooltips=[("x,y", "@x, @y")]- 设置显示格式
tooltips=[('x,y', '(@x{0,0.000},@y{0,0.000})')]
为什么 hovertool 显示多个相同显示框的
因为,不同的图形有公共点,这样就造成同一个点被重新绘制了多次
避免方法:
单独设置 hovertool.renderers
- 这是因为,默认情况下,bokeh 会把所有的图形(glyph)都添加到 hovertool.renderers 中
- 把这些需要 hovertool 显示的点,单独加入到
hovertool.renderers即可
设定那些点会触发 hovertool
即设定哪个形状上的点会触发 HoverTool
| |
解说:
- 这里把会触发数据点单独放到图形
scatter和other_scatter上 - 传递给
HoverTool.renderers属性
pareto 图绘制
| |
vbar_stack 图绘制
| |
嵌入 bokeh 绘图结果到 html 前段页面
主绘图区域大小
参考:
手动设定法:
- 使用 figure() 的 frame_width 和 frame_height 属性
问题
- 这样整个 canvas 的大小就是变动的了,需要配合 min_border 设定整个 canvas 的大小
间接设定法:
设定整个绘图区域(坐标方框)的大小
figure(with=..., height=...), 设置的是整个 convas 绘图区域的大小,包括 title 和 axis_label 等参数:
widthheight
设定边界区域最小大小
通过 figure() 的
min_border设定- 这是最小边界大小,如果内容如 title 太大,可能 title 所占的区域大小多于 min_boder 设置
- 把边界区域大小固定后,剩余的是主绘图区域
Label 对齐问题
label 的 x,y 默认是左下角的位置
文章作者
上次更新 2023-02-01 (9aed3e4)