首页 > 解决方案 > 散景中贝塞尔曲线之间的填充区域

问题描述

我正在尝试使用创建一个 sankey 图,bokeh并且目前正在尝试弄清楚如何填充两条Bezier曲线之间的区域。这是可能的还是有其他方法可以在不添加单个Bezier字形的情况下实现这一点?

下面的示例代码——目标是用橙色填充曲线之间的区域。

from bokeh.models import Range1d, Plot, LinearAxis
from bokeh.models.glyphs import Bezier
from bokeh.io import show

plot = Plot(title=None, x_range=Range1d(0, 1), y_range=Range1d(-1, 1), plot_width=300, plot_height=300)

glyph = Bezier(x0=0, y0=0, x1=1, y1=1, cx0=0.5, cy0=0.01, cx1=0.5, cy1=0.99, line_color="orange", line_width=2)
glyph2 = Bezier(x0=0, y0=-1, x1=1, y1=0.5, cx0=0.5, cy0=-0.99, cx1=0.5, cy1=0.49, line_color="orange", line_width=2)

g1 = plot.add_glyph(glyph)
g2 = plot.add_glyph(glyph2)

xaxis = LinearAxis()
plot.add_layout(xaxis, 'below')

yaxis = LinearAxis()
plot.add_layout(yaxis, 'left')

show(plot)

在此处输入图像描述

标签: pythonbokeh

解决方案


从 Bokeh0.13.0开始,无法使用任何内置功能在两条 Bézier 曲线之间进行填充。目前,为了实现这一点,您必须编写某种自定义扩展类,该类使用您的输入数据绘制曲线并使用 HTML Canvas JavaScript API 进行填充。

否则,如果您能够将曲线表示为显式多段线而不是贝塞尔曲线,那么下一个最接近的就是Band注释。假设具有 、 和 的数据框xy_meany_std可以执行以下操作:

df['lower'] = df.y_mean - df.y_std
df['upper'] = df.y_mean + df.y_std

source = ColumnDataSource(df.reset_index())

band = Band(base='x', lower='lower', upper='upper', source=source, 
            level='underlay', fill_alpha=1.0, line_width=1, line_color='black')
p.add_layout(band)

在此处输入图像描述

可以考虑添加一个更通用的波段,可以通过诸如贝塞尔曲线之类的东西来指定。随意提出GitHub 功能请求问题


推荐阅读