使用散景:如何绘制可变大小的节点和节点颜色? [英] Using bokeh: How does one plot variable size nodes, and node colors?
问题描述
我正在尝试使用具有 bokeh 12.7
I am trying to display a graph using networkx with bokeh 12.7
that
- 具有基于节点度的节点大小
- 基于另一个节点属性的颜色.
所需的输出:
import pandas as pd
import numpy as np
import networkx as nx
import seaborn as sns
from bokeh.io import show, output_notebook #output_file,
from bokeh.plotting import figure
from bokeh.models.graphs import from_networkx
from bokeh.models import GraphRenderer, StaticLayoutProvider, LinearColorMapper, ColumnDataSource
from bokeh.palettes import Spectral8, Spectral4
G = nx.karate_club_graph()
# Some Random index
node_color = {k:v for k, v in enumerate(np.random.uniform(low=0, high=21, size=(G.number_of_nodes(),)).round(1))}
# Set node attributes
nx.set_node_attributes(G, 'node_color', node_color)
nx.set_node_attributes(G, 'node_size', G.degree())
尝试将bokeh
与cubehelix_palette
一起使用
Try to graph using bokeh
with cubehelix_palette
# Map cubehelix_palette
palette = sns.cubehelix_palette(21)
pal_hex_lst = palette.as_hex()
mapper = LinearColorMapper(palette=pal_hex_lst, low=0, high=21)
# Initiate bokeh plot
plot = figure(title="Resized Node Demo", x_range=(-1.1,1.1), y_range=(-1.1,1.1),
tools="", toolbar_location=None)
# Graph renderer using nx
graph = from_networkx(G, nx.spring_layout, scale=2, center=(0,0))
# Style node
graph.node_renderer.glyph = Circle(size='node_size', fill_color={'field': 'node_color', 'transform': mapper})
plot.renderers.append(graph)
output_notebook()
#output_file("networkx_graph.html")
show(plot)
哪个出现此错误:Glyph refers to nonexistent column name
Which gives this error: Glyph refers to nonexistent column name
# 1. Create Plot container
plot = figure(title=endNode, x_range=(-1.1,1.1), y_range=(-1.1,1.1),
tools="", toolbar_location=None)
# 2. Create graph plot comtainer
graph = GraphRenderer()
node_link_dict = nx.readwrite.json_graph.node_link_data(G)
node_df = pd.DataFrame(node_link_dict['nodes'])
node_cds = ColumnDataSource.from_df(graph_data.node_df)
graph.node_renderer.data_source.data = node_cds
# 3. Set Node Style
graph.node_renderer.glyph = Circle(size='node_size', fill_color='node_color')
有什么想法吗?
推荐答案
我也尝试基于度中心性来设置节点大小,并且能够使用
I was trying to set the node size based on the degree centrality as well and was able to do so using
graph.node_renderer.data_source = source
我仍然可以看到不同的大小和颜色(请参见附件图像),尽管我仍无法找到以下错误的原因
I can see the differing sizes and colors (see attached image), although I could not find the reason for the below error yet
E-1010(CDSVIEW_SOURCE_DOESNT_MATCH):Glyph渲染器使用的CDSView必须具有与Glyph渲染器的数据源匹配的源:GlyphRenderer(id ='035dd78a-7bff-40d1-8357-d7193222ca02',...)
E-1010 (CDSVIEW_SOURCE_DOESNT_MATCH): CDSView used by Glyph renderer must have a source that matches the Glyph renderer's data source: GlyphRenderer(id='035dd78a-7bff-40d1-8357-d7193222ca02', ...)
#just to make the sizes visible
node_size = {k:5*v for k,v in G.degree().items()}
### set node attributes
nx.set_node_attributes(G, 'node_color', node_color)
nx.set_node_attributes(G, 'node_size', node_size)
source=ColumnDataSource(pd.DataFrame.from_dict({k:v for k,v in G.nodes(data=True)},orient='index'))
mapper = LinearColorMapper(palette=pal_hex_lst, low=0, high=21)
### Initiate bokeh plot
plot = figure(title="Resized Node Demo", x_range=(-1.1,1.1), y_range=(-1.1,1.1),
tools="", toolbar_location=None)
# Graph renderer using nx
graph = from_networkx(G, nx.spring_layout, scale=2, center=(0,0))
# Style node
graph.node_renderer.data_source = source
graph.node_renderer.glyph = Circle(size='node_size', fill_color={'field': 'node_color', 'transform': mapper})
plot.renderers.append(graph)
这篇关于使用散景:如何绘制可变大小的节点和节点颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!