📜  Python Bokeh - 制作交互式图例

📅  最后修改于: 2022-05-13 01:55:41.303000             🧑  作者: Mango

Python Bokeh - 制作交互式图例

Bokeh 是一个Python交互式数据可视化。它使用 HTML 和 JavaScript 渲染其绘图。它针对现代 Web 浏览器进行演示,提供具有高性能交互性的新颖图形的优雅、简洁构造。

如何制作互动传奇?

图表的图例反映了图表 Y 轴上显示的数据。在 Bokeh 中,图例对应于字形。有两种方法可以使图例交互:

  • 隐藏
  • 静音

隐藏字形

通过将图例 click_policy 属性设置为“隐藏”,可以从图例中隐藏字形。

例子 :

Python3
# importing the modules 
from bokeh.plotting import figure, output_file, show 
  
# file to save the model 
output_file("gfg.html") 
         
# instantiating the figure object 
graph = figure(title = "Bokeh Hiding Glyphs") 
  
# plotting the graph
graph.vbar(x = 1,  top = 5,
           width = 1, color = "violet",
           legend_label = "Violet Bar")
graph.vbar(x = 2,  top = 5,
           width = 1, color = "green",
           legend_label = "Green Bar")
graph.vbar(x = 3,  top = 5,
           width = 1, color = "yellow",
           legend_label = "Yellow Bar")
graph.vbar(x = 4,  top = 5,
           width = 1, color = "red",
           legend_label = "Red Bar")
  
# enable hiding of the glyphs
graph.legend.click_policy = "hide"
  
# displaying the model 
show(graph)


Python3
# importing the modules 
from bokeh.plotting import figure, output_file, show 
  
# file to save the model 
output_file("gfg.html") 
         
# instantiating the figure object 
graph = figure(title = "Bokeh Hiding Glyphs") 
  
# plotting the graph
graph.vbar(x = 1,  top = 5,
           width = 1, color = "violet",
           legend_label = "Violet Bar",
           muted_alpha=0.2)
graph.vbar(x = 2,  top = 5,
           width = 1, color = "green",
           legend_label = "Green Bar",
           muted_alpha=0.2)
graph.vbar(x = 3,  top = 5,
           width = 1, color = "yellow",
           legend_label = "Yellow Bar",
           muted_alpha=0.2)
graph.vbar(x = 4,  top = 5,
           width = 1, color = "red",
           legend_label = "Red Bar",
           muted_alpha=0.2)
  
# enable hiding of the glyphs
graph.legend.click_policy = "mute"
  
# displaying the model 
show(graph)


输出 :

静音字形

隐藏字形使其完全消失,另一方面,使字形静音只是基于参数不强调字形。通过将图例 click_policy 属性设置为“静音”,可以将字形从图例中静音。

Python3

# importing the modules 
from bokeh.plotting import figure, output_file, show 
  
# file to save the model 
output_file("gfg.html") 
         
# instantiating the figure object 
graph = figure(title = "Bokeh Hiding Glyphs") 
  
# plotting the graph
graph.vbar(x = 1,  top = 5,
           width = 1, color = "violet",
           legend_label = "Violet Bar",
           muted_alpha=0.2)
graph.vbar(x = 2,  top = 5,
           width = 1, color = "green",
           legend_label = "Green Bar",
           muted_alpha=0.2)
graph.vbar(x = 3,  top = 5,
           width = 1, color = "yellow",
           legend_label = "Yellow Bar",
           muted_alpha=0.2)
graph.vbar(x = 4,  top = 5,
           width = 1, color = "red",
           legend_label = "Red Bar",
           muted_alpha=0.2)
  
# enable hiding of the glyphs
graph.legend.click_policy = "mute"
  
# displaying the model 
show(graph) 

输出 :