如何使用图例点击策略隐藏Python Bokeh图中的直线和圆?

问题描述 投票:0回答:2

我有一个Python Bokeh图(多条图线+圆形,如下所示:

 output_notebook()

source = ColumnDataSource(da)

col_names = paises

p = figure(
 title = "A",
 x_axis_type="datetime",
 plot_width=800,
 plot_height=400,
 sizing_mode='scale_width',
 toolbar_location='above',
 tools ="box_zoom,reset,wheel_zoom" )

p_dict = dict()


for col, c, col_name in zip(da.columns, color, col_names):
 p_dict[col_name] = p.circle('date', col, source=source, color=c, size=3.5, line_width=0.5, fill_color=None)
 p_dict[col_name] = p.line('date', col, source=source, color=c, line_width=1) 

 p.add_tools(HoverTool( 
     toggleable=False,
     renderers=[p_dict[col_name]],
     tooltips=[('datetime','@date{%F}'),(col, f'@{col}')],
     formatters={'@date': 'datetime'}
 ))


legend = Legend(items=[(x, [p_dict[x]]) for x in p_dict])


p.add_layout(legend)

p.legend.click_policy="hide"

p.legend.label_text_font_size = "1vw"
p.legend.location = 'top_left'
p.left[0].formatter.use_scientific = False

show(p)

我正在使用legend.click_policy="hide"隐藏线,但是单击图例时,仅隐藏圆而不隐藏线。有没有办法同时隐藏直线和圆圈?

谢谢!

python plot bokeh
2个回答
0
投票

如果可以避免手动构造图例,则只需为字形函数提供相同的legend_name


0
投票

我意识到我必须发布完整的代码(我已经对其进行了更改,以便使问题易于理解)。我找到了将图例添加到for循环的解决方案:

© www.soinside.com 2019 - 2024. All rights reserved.