python - 填充 Bokeh 中贝塞尔曲线之间的区域

标签 python bokeh

我正在尝试使用 Bokeh 创建桑基图,并且目前正在尝试找出如何填充两条贝塞尔曲线之间的区域。这是否可能,或者是否有其他方法可以在不添加单独的贝塞尔曲线的情况下实现此目的?

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

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)

enter image description here

最佳答案

从 Bokeh 0.13.0 开始,无法使用任何内置功能在两条贝塞尔曲线之间进行填充。目前,要实现此目的,您必须编写某种 custom extension class它获取您的输入数据并绘制曲线并使用 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)

enter image description here

也许可以考虑添加一个更通用的频带,可以通过贝塞尔曲线等来指定。随意做一个GitHb feature request issue

关于python - 填充 Bokeh 中贝塞尔曲线之间的区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50955353/

相关文章:

javascript - Flask后端如何处理大量JSON数据输出

python-3.x - 用于图中第二个图的 Bokeh Hovertool

python - 通过类内的 For 循环使用 linspace 进行迭代

python - 使用 PrintfTickFormatter 将 Bokeh x 轴从十进制格式化为百分比

python - IPython Notebook 小部件的 Bokeh : extra figures apppearing

python - 使用 Bokeh 绘制具有不同半径(不同大小)和基于值的不同颜色的纬度/经度点

Bokeh :模型只能由一个文档所有

python - 如何在 Python 中下载谷歌图片搜索结果

python - 在没有名称总和的 Pandas 数据框中添加总计行?

Python 字符串操作问题