python - 在 Python Bokeh 中填充线条下方的区域

标签 python bokeh

如何在 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) 

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

enter image description here

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

最佳答案

也许是这样的:

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

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

完整的示例如下:

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)

生成的图:Generated plot

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

fig.varea(source=plot_source, x="date", y1=0, y2="cost",
          alpha=0.2, fill_color='#55FF88', legend_label="cost")

关于python - 在 Python Bokeh 中填充线条下方的区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58718224/

相关文章:

python - 当用户在数据表中单击时, Bokeh 图将被停用,并且没有正确的方法来激活 Bokeh 图

python 在同一连接上套接多条消息

python - 使用 Numpy,如何计算数字 1 到 10 的 25 个百分位数?

python - 如何正确部署需要服务器回调的 Bokeh 应用程序?

python - 动态更新 Holoviz 面板布局

Python bokeh修改 Axis 刻度

python - 使用 bokeh 或 matplotlib 的 Pandas DataFrame 分层饼图/ donut chart

python - 如何根据两个条件替换数据框中的所有值

python - 如何在 linux 中使用命令动态更改日期

python - Python 中具有 sigma 裁剪的二维数组的中值