线条和曲线#

单行#

下面的示例展示了如何使用 line() 方法从一维序列 xy 点生成单线图:

from bokeh.plotting import figure, show

p = figure(width=400, height=400)

# add a line renderer
p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2)

show(p)

步骤线#

对于某些类型的数据,数据点之间的离散步骤可能比线性段效果更好。要生成这种类型的数据表示,请使用step()字形方法。

from bokeh.plotting import figure, show

p = figure(width=400, height=400)

# add a steps renderer
p.step([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2, mode="center")

show(p)

调整mode参数,以在每个步骤之前、之后或中间绘制步骤级别的x坐标。

多行#

如果你想一次性绘制多条线,请使用multi_line()字形方法,如下所示:

from bokeh.plotting import figure, show

p = figure(width=400, height=400)

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_width=4)

show(p)

注意

与许多其他字形方法不同,multi_line() 接受每条线的 xy 位置的列表。 multi_line() 方法还期望为每条线提供一个标量值或标量值列表,用于参数如颜色、透明度和线宽。 同样,您可以使用由点坐标列表和匹配长度的标量值列表组成的 ColumnDataSource

缺失点#

你可以将NaN值传递给line()multi_line()图形。这会产生带有NaN值间隙的断线。

from math import nan

from bokeh.plotting import figure, show

p = figure(width=400, height=400)

# add a line renderer with NaN values
p.line([1, 2, 3, nan, 4, 5], [6, 7, 2, 4, 4, 5], line_width=2)

show(p)

堆叠线#

在处理时间序列的百分比和其他类似数据时,您可能希望将具有共同索引的线条堆叠起来。为此,您可以使用vline_stack()hline_stack()这两个便捷方法。

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

source = ColumnDataSource(data=dict(
    x=[1, 2, 3, 4, 5],
    y1=[1, 2, 4, 3, 4],
    y2=[1, 4, 2, 2, 3],
))
p = figure(width=400, height=400)

p.vline_stack(['y1', 'y2'], x='x', source=source)

show(p)

注意

本章中的这个例子和其他例子依赖于 ColumnDataSource 进行数据结构化。有关如何使用此数据结构的信息,请参阅 数据源

与标记结合#

你可以通过在单个figure()上调用它们的方法来在单个图表上组合多个图形。

from bokeh.plotting import figure, show

x = [1, 2, 3, 4, 5]
y = [6, 7, 8, 7, 3]

p = figure(width=400, height=400)

# add both a line and circles on the same plot
p.line(x, y, line_width=2)
p.scatter(x, y, fill_color="white", size=8)

show(p)

这一原则适用于所有bokeh.plotting的图形方法。您可以根据需要向Bokeh图中添加任意数量的图形。

特殊字形#

分段#

要绘制多个单独的线段,请使用 segment()ray() 字形方法。

segment() 方法接受起点 x0y0 以及终点 x1y1。它会在这些点之间渲染线段。

from bokeh.plotting import figure, show

p = figure(width=400, height=400)
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_width=3)

show(p)

光线#

ray() 方法接受起始点 xy,以及一个 length(以屏幕单位)和一个 angleangle_units 参数默认为 "rad",但你也可以将其设置为 "deg",以便角度以度而不是弧度来测量。要有一个“无限”的射线,始终延伸到图表的边缘,请将 length 设置为 0

from bokeh.plotting import figure, show

p = figure(width=400, height=400)
p.ray(x=[1, 2, 3], y=[1, 2, 3], length=45, angle=[30, 45, 60],
      angle_units="deg", color="#FB8072", line_width=2)

show(p)

跨度#

要绘制多个水平或垂直跨度(分别是无限宽度或高度的线),请使用hspan()vspan()字形方法。这些方法分别接受yx坐标分量。请注意,这些字形只能在一个轴上计算边界,因此可能需要在正交轴上明确指定范围,例如,如果单独使用。

from 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_color="red",
)
plot.vspan(
    x=[0, 5, 15, 33],
    line_width=[1, 2, 3, 4], line_color="blue",
)

show(plot)

弧线#

要绘制一个简单的弧线,请使用arc()图形方法,该方法接受radiusstart_angleend_angle来确定位置。此外,direction属性决定了在起始角度和结束角度之间是顺时针("clock")还是逆时针("anticlock")渲染。

from bokeh.plotting import figure, show

p = figure(width=400, height=400)
p.arc(x=[1, 2, 3], y=[1, 2, 3], radius=0.1, start_angle=0.4, end_angle=4.8, color="navy")

show(p)

参数化#

要绘制参数化的二次和三次曲线,请使用 quadratic()bezier() 字形方法。有关这些曲线的更多详细信息,请参阅 参考文档