我正在尝试使用带有 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')
有什么想法吗?
答案 0 :(得分:3)
我试图根据度数中心性设置节点大小,并且能够使用
进行设置graph.node_renderer.data_source = source
我可以看到不同的尺寸和颜色(见附图),虽然我找不到以下错误的原因
E-1010(CDSVIEW_SOURCE_DOESNT_MATCH):Glyph渲染器使用的CDSView必须具有与Glyph渲染器的数据源匹配的源:GlyphRenderer(id =' 035dd78a-7bff-40d1-8357-d7193222ca02&#39 ;,...)
#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)
答案 1 :(得分:0)
正在寻找类似的东西。也许对别人有帮助。
您可以通过更改节点大小的相同方法来进行更改。因此,没有必要使用也会混在一起的熊猫。如果使用错误的方式。
您可以创建一个列表,其中存储了每个节点的颜色。 然后添加颜色信息:
function __construct()
{
parent::__construct();
$this->load->library('session');
if ($this->session->userdata('admin_user')=='' and
$this->uri->segment(2) !== 'admin_login'))
{
redirect(base_url().'adminpanel04736/admin_login');
}
}
或者,如果您确实要使用节点属性,则可以使用字典来执行此操作: 首先创建所有颜色的列表:
colors = [...]
graph.node_renderer.data_source.data['colors'] = colors
graph.node_renderer.glyph = Circle(size='degrees', fill_color='colors')