Bokeh - 使用 Select 组件和 CustomJS 更新图表
创始人
2024-12-23 11:00:29
0

以下是使用 Select 组件和 CustomJS 更新图表的 Bokeh 代码示例:

from bokeh.io import output_file, show
from bokeh.layouts import row, column
from bokeh.models import Select, CustomJS
from bokeh.plotting import figure
from bokeh.sampledata.iris import flowers

# 创建一个包含所有不同种类的列表
species = flowers['species'].unique().tolist()

# 创建一个 Select 组件
select = Select(title="Species", options=species)

# 创建一个空的图表
p = figure(width=400, height=400)

# 创建一个自定义回调函数
callback = CustomJS(args=dict(source=select, plot=p), code="""
    // 获取 Select 组件当前的值
    var species = source.value;
    
    // 根据选择的值过滤数据
    var new_data = data[data['species'] == species];
    
    // 更新图表的数据
    plot.data_source.data = new_data;
    plot.change.emit();
""")

# 将回调函数绑定到 Select 组件的值变化事件上
select.js_on_change('value', callback)

# 初始化图表的数据
data = flowers[flowers['species'] == species[0]]

# 绘制散点图
p.circle(x='petal_length', y='petal_width', source=data, size=10)

# 将 Select 组件和图表放在一起显示
layout = row(column(select), p)

# 输出到 HTML 文件并显示
output_file("bokeh_select_example.html")
show(layout)

运行此代码将生成一个包含 Select 组件和散点图的 HTML 文件,并且当选择不同的物种时,图表将根据选择的物种更新数据。

相关内容

热门资讯

黑科技插件!微乐小程序免费黑科... 黑科技插件!微乐小程序免费黑科技,微信小程序微乐辅助器脚本(透视)总是是有辅助插件(哔哩哔哩)所有人...
透视绝活儿!wpk俱乐部有没有... 透视绝活儿!wpk俱乐部有没有辅助(WePoKer软件)真是有辅助工具(哔哩哔哩)1、wpk俱乐部有...
透视好友房!微乐小程序黑科技,... 透视好友房!微乐小程序黑科技,微乐智能插件激活码(透视)本来是真的辅助技巧(哔哩哔哩)暗藏猫腻,小编...
透视方针!hhpoker脚本(... 透视方针!hhpoker脚本(WePoKer脚本)真是真的是有辅助方法(哔哩哔哩)该软件可以轻松地帮...
辅助透视!微乐小程序黑科技免费... 辅助透视!微乐小程序黑科技免费,微信微乐自建房脚本免费下载(透视)其实是真的辅助app(哔哩哔哩)所...
透视法子!wepoker有没有... 透视法子!wepoker有没有辅助(HHpoker透视挂)竟然真的是有辅助工具(哔哩哔哩)1)wep...
透视模拟器!微信微乐游戏辅助脚... 透视模拟器!微信微乐游戏辅助脚本,微乐自建房辅助入口官网(透视)好像是真的辅助器(哔哩哔哩)1、下载...
透视模块!pokemomo辅助... 透视模块!pokemomo辅助工具(WePoKer机器人)一贯是真的有辅助神器(哔哩哔哩)pokem...
透视ai!微乐小程序黑科技,微... 透视ai!微乐小程序黑科技,微乐自建房插件免费软件(透视)好像是有辅助工具(哔哩哔哩)该软件可以轻松...
透视秘籍!hhpoker有没有... 透视秘籍!hhpoker有没有作必弊挂(WePoKer规律)一直是真的有辅助技巧(哔哩哔哩);1、全...