在 Python Bokeh 中填充一条线下的区域

isa*_*lla 4 python bokeh

如何在散景中对一条线下的区域进行阴影处理?

我有一个简单的线图如下,我想用指定的颜色填充一条线。

import pandas as pd
from bokeh.io import output_file, curdoc
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource

output_file("layout.html")


df = pd.DataFrame({'date': ["1-1-2019", "2-1-2019", "3-1-2019", "4-1-2019", "5-1-2019", "6-1-2019", "7-1-2019", "8-1-2019", "9-1-2019", "10-1-2019"],
                   'cost': [10, 15, 20, 30, 25, 5, 15, 30, 35, 25]})

fig = figure(x_axis_type="datetime", toolbar_location='above', plot_width=1500)

plot_source = ColumnDataSource(data=dict(date=pd.to_datetime(df["date"]),
                                         cost=df["cost"]))

line_plot = fig.line("date", "cost", source=plot_source, line_width=2, color="#00CED1")

show(fig) 
Run Code Online (Sandbox Code Playgroud)

所以它看起来像下图中的绿色区域。

在此处输入图片说明

我检查了Pathces字形功能,但我不清楚。感谢任何帮助!

big*_*dot 6

Band是一个选项,但varea可能更好,因为它是字形而不是注释,并且varea_stack如果需要堆叠,还支持使用堆叠。

from bokeh.plotting import figure, show

p = figure(plot_width=400, plot_height=400)

p.varea(x=[1, 2, 3, 4, 5],
        y1=0, y2=[1, 4, 2, 2, 3], alpha=0.6)

show(p)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


Joh*_*anC 5

也许是这样的:

band = Band(base='date', upper='cost', source=plot_source, level='underlay',
            fill_alpha=0.2, fill_color='#55FF88')
fig.add_layout(band)
Run Code Online (Sandbox Code Playgroud)

编辑:fill_alpha=0.2, fill_color='#55FF88'似乎更接近示例

一个完整的例子如下所示:

band = Band(base='date', upper='cost', source=plot_source, level='underlay',
            fill_alpha=0.2, fill_color='#55FF88')
fig.add_layout(band)
Run Code Online (Sandbox Code Playgroud)

生成的情节: 生成的情节

PS:Band 注释不支持图例。要获得图例,请varea按照@bigreddot 的建议将 Band 替换为字形。在您的代码中:

import pandas as pd
from bokeh.io import output_file, curdoc
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, Band

output_file("layout.html")
df = pd.DataFrame({'date': ["1-1-2019", "2-1-2019", "3-1-2019", "4-1-2019", "5-1-2019", "6-1-2019", "7-1-2019", "8-1-2019", "9-1-2019", "10-1-2019"],
                   'cost': [10, 15, 20, 30, 25, 5, 15, 30, 35, 25]})
fig = figure(x_axis_type="datetime", toolbar_location='above', plot_width=1500)
plot_source = ColumnDataSource(data=dict(date=pd.to_datetime(df["date"]),
                                         cost=df["cost"]))
line_plot = fig.line("date", "cost", source=plot_source, line_width=2, color="#00CED1")
band = Band(base='date', upper='cost', source=plot_source, level='underlay',
            fill_alpha=0.2, fill_color='#55FF88')
fig.add_layout(band)
show(fig)
Run Code Online (Sandbox Code Playgroud)