You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Bokeh绘制网络节点时如何根据温度数据有无设置不同颜色

你只需要在DataFrame中提前根据温度数据的有无生成对应的颜色标识列,再将该列传入ColumnDataSource即可实现指定规则的节点配色,无需调用调色板:

假设你Excel中存储温度数据的列名为temperature,可以按需修改为实际列名,修改后的可运行代码如下:

import pandas
import numpy as np
from bokeh.io import output_notebook, show
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
output_notebook()

# 读取原始数据
df = pandas.read_excel('Pasta1.xlsx', engine='openpyxl')

# 新增节点颜色列:存在温度数据赋值红色,无温度数据赋值蓝色
# 可根据实际温度字段名、空值判定规则调整判断逻辑
df['node_color'] = np.where(df['temperature'].isna(), 'blue', 'red')

# 将颜色字段加入数据源
source = ColumnDataSource(data=dict(x = df['x'] ,y = df['y'], fill_color = df['node_color']))

# 可按需将温度字段加入提示框,方便交互时查看
TOOLTIPS = [("index", "$index"),("(x,y)", "($x, $y)"), ("温度", "@temperature")]

p = figure(width=1000, height=500, tooltips=TOOLTIPS,title="Redes")

# fill_color直接绑定数据源中的颜色字段即可
p.circle('x', 'y', size=10, fill_color='fill_color', source=source)

show(p)

核心调整说明

  • 新增node_color字段,通过条件判断直接给每个节点分配指定颜色
  • 把颜色字段加入ColumnDataSource,绘制节点时直接绑定对应字段即可生效
    如果你的温度空值不是pandas默认的NaN格式,只需要调整df['temperature'].isna()的判断规则即可,比如温度字段为空字符串的话就改成df['temperature'] == ''。

数据展示图1
数据展示图2

内容的提问来源于stack exchange,提问作者Elle Oliver

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 07:36:06