如何使用图例点击策略隐藏 Python 散景图中的直线和圆圈?
How to hide both line and circle in Python Bokeh plot using legend click policy?
我有一个Python散景图(多条线+圆)如下:
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"
来隐藏线条,但是当单击图例时,只隐藏圆而不隐藏线条。有没有办法同时隐藏线和圆?
谢谢!
如果你能避免手动构建图例,你可以只提供相同的字形函数legend_name
:
from bokeh.io import show
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
source = ColumnDataSource(dict(x=[0, 1, 2], y=[0, 2, 1]))
p = figure()
p.line('x', 'y', source=source, legend_label='line_with_circles')
p.circle('x', 'y', source=source, legend_label='line_with_circles')
p.legend.click_policy = "hide"
p.legend.location = 'top_left'
show(p)
如果您必须手动创建图例,只需创建一个图例项并为它提供两个渲染器。
我意识到我必须 post 完整的代码(我已经更改了它以便很好地理解问题)。我找到了将图例添加到 for 循环的解决方案:
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, leg in zip(da.columns, color, col_names, paises):
p_dict[col_name] = p.circle('date', col, source=source, color=c, size=3.5, line_width=0.5, fill_color=None, legend_label = leg)
p_dict[col_name] = p.line('date', col, source=source, color=c, line_width=1, legend_label = leg)
p.add_tools(HoverTool(
toggleable=False,
renderers=[p_dict[col_name]],
tooltips=[('datetime','@date{%F}'),(col, f'@{col}')],
formatters={'@date': 'datetime'}
))
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)
我有一个Python散景图(多条线+圆)如下:
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"
来隐藏线条,但是当单击图例时,只隐藏圆而不隐藏线条。有没有办法同时隐藏线和圆?
谢谢!
如果你能避免手动构建图例,你可以只提供相同的字形函数legend_name
:
from bokeh.io import show
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
source = ColumnDataSource(dict(x=[0, 1, 2], y=[0, 2, 1]))
p = figure()
p.line('x', 'y', source=source, legend_label='line_with_circles')
p.circle('x', 'y', source=source, legend_label='line_with_circles')
p.legend.click_policy = "hide"
p.legend.location = 'top_left'
show(p)
如果您必须手动创建图例,只需创建一个图例项并为它提供两个渲染器。
我意识到我必须 post 完整的代码(我已经更改了它以便很好地理解问题)。我找到了将图例添加到 for 循环的解决方案:
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, leg in zip(da.columns, color, col_names, paises):
p_dict[col_name] = p.circle('date', col, source=source, color=c, size=3.5, line_width=0.5, fill_color=None, legend_label = leg)
p_dict[col_name] = p.line('date', col, source=source, color=c, line_width=1, legend_label = leg)
p.add_tools(HoverTool(
toggleable=False,
renderers=[p_dict[col_name]],
tooltips=[('datetime','@date{%F}'),(col, f'@{col}')],
formatters={'@date': 'datetime'}
))
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)