Bokeh 折线与曲线绘制全指南line、step、multi_line 与专用 glyph 实战【免费下载链接】bokehInteractive Data Visualization in the browser, from Python项目地址: https://gitcode.com/GitHub_Trending/bo/bokeh导读本文聚焦 Bokeh 中所有与「线」相关的绘图能力从最基础的line单条折线到step阶梯线、multi_line多条线、基于NaN的断点线、vline_stack/hline_stack堆叠线再到segment、ray、hspan/vspan、arc、quadratic/bezier等专用几何 glyph。读完本文你将掌握 Bokeh 折线家族的完整 API 用法、ColumnDataSource数据组织方式以及缺失点处理等关键细节并能直接照搬仓库中的可运行示例。本文内容以 用户指南 lines 章节 为骨架并结合 glyph_api.py 与 _figure.py 源码进行纵深说明。一、前置准备figure 与 showBokeh 折线绘制的统一入口是bokeh.plotting模块下的figure()与show()from bokeh.plotting import figure, show p figure(width400, height400) # 在 p 上调用各种 glyph 方法…… show(p)figure()创建一张图Figure对象所有线类 glyph 方法都在其上调用show(p)将图渲染到浏览器或 notebook。后续所有示例均遵循这一模式部分示例配合ColumnDataSource组织数据详见 数据章节。二、单条折线line当数据是一维的x、y点序列时使用lineglyph 方法绘制单条折线。仓库示例 line_single.pyfrom bokeh.plotting import figure, show p figure(width400, height400) # add a line renderer p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width2) show(p)p.line(x, y, ...)会在相邻数据点之间绘制直线段line_width等样式参数直接控制线条外观。从源码看line定义于 glyph_api.py 的line方法其底层对应bokeh.models.glyphs.Line模型返回值是GlyphRenderer因此返回对象可继续挂接legend_label、level等渲染器级属性。三、阶梯线step与 mode 参数对于离散数据如阶跃变化的过程量线性连接不如阶梯表示直观。此时使用stepglyph 方法。仓库示例 line_steps.pyfrom bokeh.plotting import figure, show p figure(width400, height400) # add a steps renderer p.step([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width2, modecenter) show(p)step方法定义于 glyph_api.py关键在于mode参数它决定每个阶梯的竖直段落在水平坐标上的对齐方式mode 取值含义before阶梯水平段在 x 坐标之前左对齐after阶梯水平段在 x 坐标之后右对齐center阶梯水平段以 x 坐标为中点的中心对齐示例中使用modecenter即每一步的水平段以数据点的 x 值为中心展开。step常与ColumnDataSource配合只需将x/y替换为数据源中的列名字符串即可。四、多条线一次绘制multi_linemulti_line允许在一次调用中绘制多条折线它与大多数 glyph 方法不同——x和y接受「列表的列表」每个内部列表对应一条线的坐标序列。仓库示例 line_multiple.pyfrom bokeh.plotting import figure, show p figure(width400, height400) p.multi_line([[1, 3, 2], [3, 4, 6, 6]], [[2, 1, 4], [4, 7, 8, 5]], color[firebrick, navy], alpha[0.8, 0.3], line_width4) show(p)使用要点来自文档明确说明与多数 glyph 方法不同multi_line的x/y必须为列表的列表每条线一组坐标对于color、alpha、line_width等参数multi_line期望传入标量所有线共享或与线数量等长的标量列表逐线指定样式同样可以使用ColumnDataSource数据源中存放「点坐标的列表的列表」列以及「长度匹配的标量值列表」列例如from bokeh.models import ColumnDataSource from bokeh.plotting import figure, show source ColumnDataSource(datadict( xs[[1, 3, 2], [3, 4, 6, 6]], ys[[2, 1, 4], [4, 7, 8, 5]], colors[firebrick, navy], widths[4, 4], )) p figure(width400, height400) p.multi_line(xsxs, ysys, colorcolors, line_widthwidths, sourcesource) show(p)multi_line对应源码 glyph_api.py底层模型为bokeh.models.glyphs.MultiLine。五、缺失点处理NaN与None的陷阱绘制时间序列时经常遇到缺失数据。Bokeh 允许向line与multi_line传入NaN值从而产生带缺口的断线。仓库示例 line_missing_points.pyfrom math import nan from bokeh.plotting import figure, show p figure(width400, height400) # add a line renderer with NaN values p.line([1, 2, 3, nan, 4, 5], [6, 7, 2, 4, 4, 5], line_width8, legend_labelline with NaN, alpha0.5) # dont use None as a value for a renderer, because it will be drawn as 0 p.line([1, 2, 3, None, 4, 5], [6, 7, 2, 4, 4, 5], line_width2, legend_labelline with None (BAD), line_dashdashed, colorred) show(p)关键警告文档明确强调NaN必须来自math或numpy如from math import nan或numpy.nan绝不使用 Python 原生NoneNone会被求值为0并照常绘制出来不会产生缺口还会隐性影响图形例如改变图表的坐标范围让折线出现「跌到 0」的假象示例中用红色虚线标出这种错误用法。这是 Bokeh 数据处理中一个高频踩坑点从 pandas 读取含缺失值的列时缺失通常表现为NaN可直接使用但若数据中混入了None务必先清洗为NaN再传入 glyph。六、堆叠折线vline_stack与hline_stack当处理百分比等时间序列、希望多条线共享一个公共坐标轴并逐层堆叠时可使用vline_stack与hline_stack便捷方法。仓库示例 vline_stack.pyfrom bokeh.models import ColumnDataSource from bokeh.plotting import figure, show source ColumnDataSource(datadict( x[1, 2, 3, 4, 5], y1[1, 2, 4, 3, 4], y2[1, 4, 2, 2, 3], )) p figure(width400, height400) p.vline_stack([y1, y2], xx, sourcesource) show(p)vline_stack将stackers数据源列名列表中的各列作为y坐标依次累加堆叠共用x坐标hline_stack则反之将各列堆叠为x坐标。源码级原理从 _figure.py 源码可以看到vline_stack定义于 _figure.py#L657内部调用self._line_stack(stackers, x, **kw)即沿y 方向堆叠、共用 x 轴hline_stack定义于 _figure.py#L522内部调用self._line_stack(stackers, y, **kw)即沿x 方向堆叠、共用 y 轴。两者都返回list[GlyphRenderer]并有几个值得注意的行为每个生成的渲染器的name会被设置为对应 stacker 的列名——这在 hover 工具中配合特殊变量$name可直接显示「这条线是哪一列」除stackers外的关键字参数都会透传给每次line调用若某个关键字的值是列表/元组则每次调用按顺序取一个值例如p.vline_stack([y1, y2], xx, color[blue, red], sourcesource)会为两条线分别着色文档说明等价展开形式p.vline_stack([y1,y2], ...)相当于依次调用p.line(ystack(y1), ...)与p.line(ystack(y1,y2), ...)其中stack实现累加。本示例与本章其余示例都依赖ColumnDataSource组织数据其详细用法见数据章节。七、组合多种 glyph线与标记叠加Bokeh 的图允许叠加任意多个 glyph——只需在同一个figure对象上依次调用不同的 glyph 方法。仓库示例 multiple_glyphs.pyfrom bokeh.plotting import figure, show x [1, 2, 3, 4, 5] y [6, 7, 8, 7, 3] p figure(width400, height400) # add both a line and circles on the same plot p.line(x, y, line_width2) p.scatter(x, y, fill_colorwhite, size8) show(p)先画一条折线再在同一坐标上叠加白色填充的散点标记形成「折线 数据点」的经典展示。这一原则适用于bokeh.plotting中的所有 glyph 方法——你可以在一个图上添加任意数量的 glyph 渲染器这也为后续叠加segment、ray、arc等专用几何提供了组合基础。八、专用线 glyph8.1 Segmentssegment需要绘制多条独立线段时使用segment方法它接收起点x0、y0与终点x1、y1并在每对起终点之间渲染线段。仓库示例 segment.pyfrom bokeh.plotting import figure, show p figure(width400, height400) p.segment(x0[1, 2, 3], y0[1, 2, 3], x1[1.2, 2.4, 3.1], y1[1.2, 2.5, 3.7], color#F4A582, line_width3) show(p)8.2 Raysrayray从起点x、y出发按指定length以屏幕单位计和angle绘制射线对应源码 glyph_api.py。仓库示例 ray.pyfrom bokeh.plotting import figure, show p figure(width400, height400) p.ray(x[1, 2, 3], y[1, 2, 3], length45, angle[30, 45, 60], angle_unitsdeg, color#FB8072, line_width2) show(p)关键参数说明length射线长度单位是屏幕像素screen units而非数据坐标单位angle射线方向角angle_units默认rad弧度可设为deg以使用角度制示例中即传入了 30°、45°、60°无限射线技巧将length设为0射线将一直延伸到绘图区域的边缘适合做趋势指示线。8.3 Spanshspan与vspanhspan/vspan分别绘制无限宽的水平线与无限高的垂直线hspan只接受y坐标分量vspan只接受x坐标分量。仓库示例 spans.pyfrom bokeh.io import show from bokeh.plotting import figure plot figure() plot.hspan( y[0, 5, 15, 33], line_width[1, 2, 3, 4], line_colorred, ) plot.vspan( x[0, 5, 15, 33], line_width[1, 2, 3, 4], line_colorblue, ) show(plot)注意文档明确提示这类 glyph只能在一个轴向上计算边界因此当它们单独出现在图中时可能需要在正交轴上显式指定坐标范围如figure(y_range(0, 40))否则绘图区域的边界可能无法自动确定。示例中传入标量列表即可为每条 span 独立设置线宽与颜色。8.4 Arcsarcarc用于绘制简单的圆弧通过radius、start_angle、end_angle定位对应源码 glyph_api.py。仓库示例 arcs.pyfrom bokeh.plotting import figure, show p figure(width400, height400) p.arc(x[1, 2, 3], y[1, 2, 3], radius0.1, start_angle0.4, end_angle4.8, colornavy) show(p)direction属性决定弧的走向clock表示从起始角到终止角顺时针渲染anticlock表示逆时针渲染。需要同时绘制多段不同起止角度的圆弧时传入等长列表即可如示例中三条弧共享一个radius0.1标量。8.5 参数化曲线quadratic与bezier若需绘制参数化的二次与三次曲线使用quadratic与bezierglyph 方法对应源码 glyph_api.py 与 glyph_api.pyquadratic二次贝塞尔曲线需要起点x0/y0、终点x1/y1以及控制点cx/cybezier三次贝塞尔曲线需要起点、终点以及两个控制点cx0/cy0、cx1/cy1。两者的完整签名与属性说明可参见bokeh.plotting.figure的参考文档。九、小结选择哪种线 glyph场景推荐方法关键参数单条折线lineline_width、line_dash等样式离散阶跃数据stepmodebefore/after/center一次绘制多条折线multi_line列表的列表坐标 逐线样式列表带缺失点的折线line/multi_lineNaN禁用None共享坐标的堆叠时间序列vline_stack/hline_stackstackers列名列表 source多条独立线段segmentx0/y0/x1/y1带方向射线raylength屏幕单位、angle、angle_units无限水平/垂直线hspan/vspany或x注意正交轴 range圆弧arcradius、start_angle、end_angle、direction二次/三次曲线quadratic/bezier起终点 控制点所有方法都定义在 glyph_api.pyfigure的 glyph 接口与 _figure.py堆叠便捷方法中底层模型位于 src/bokeh/models/glyphs 的 glyph 模块。示例代码集中在 examples/basic/lines/ 目录可直接运行验证若需在图中补充交互能力如 hover 显示坐标、图例点击隐藏可进一步参考交互工具相关章节。【免费下载链接】bokehInteractive Data Visualization in the browser, from Python项目地址: https://gitcode.com/GitHub_Trending/bo/bokeh创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
