使用散景:如何绘制可变大小的节点和节点颜色?
Using bokeh: How does one plot variable size nodes, and node colors?
我正在尝试使用带有 bokeh 12.7
的 networkx 显示图表
- 具有基于节点度数的节点大小
- 基于另一个节点属性的颜色。
期望的输出:
数据设置
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
绘制图表
# 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
我也试过这个:
# 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')
有什么想法吗?
我也试图根据度中心性设置节点大小,并且能够使用
graph.node_renderer.data_source = source
我可以看到不同的尺寸和颜色(见附图),虽然我还没有找到以下错误的原因
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)
正在寻找类似的东西。也许对其他人有帮助。
您可以像更改节点大小一样更改它。所以没有必要使用 pandas 这也可能混淆某事。如果用错了。
您可以创建一个列表,其中存储每个节点的颜色。
然后添加颜色信息:
colors = [...]
graph.node_renderer.data_source.data['colors'] = colors
graph.node_renderer.glyph = Circle(size='degrees', fill_color='colors')
或者如果你真的想使用节点属性,那么你可以使用字典来实现:
首先创建一个包含所有颜色的列表:
colors = [...]
colors = dict(zip(G.nodes, colors))
nx.set_node_attributes(G, {k:v for k,v in colors.items()},'colors' )
graph.node_renderer.glyph = Circle(size='degrees', fill_color='colors')
我正在尝试使用带有 bokeh 12.7
的 networkx 显示图表
- 具有基于节点度数的节点大小
- 基于另一个节点属性的颜色。
期望的输出:
数据设置
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
绘制图表
# 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
我也试过这个:
# 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')
有什么想法吗?
我也试图根据度中心性设置节点大小,并且能够使用
graph.node_renderer.data_source = source
我可以看到不同的尺寸和颜色(见附图),虽然我还没有找到以下错误的原因
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)
正在寻找类似的东西。也许对其他人有帮助。
您可以像更改节点大小一样更改它。所以没有必要使用 pandas 这也可能混淆某事。如果用错了。
您可以创建一个列表,其中存储每个节点的颜色。 然后添加颜色信息:
colors = [...]
graph.node_renderer.data_source.data['colors'] = colors
graph.node_renderer.glyph = Circle(size='degrees', fill_color='colors')
或者如果你真的想使用节点属性,那么你可以使用字典来实现: 首先创建一个包含所有颜色的列表:
colors = [...]
colors = dict(zip(G.nodes, colors))
nx.set_node_attributes(G, {k:v for k,v in colors.items()},'colors' )
graph.node_renderer.glyph = Circle(size='degrees', fill_color='colors')