我设法解决了一半的问题所以我修改了这篇文章,我现在遇到的唯一问题是没有记录的错误类型找到解决方案,如果有人知道为什么会发生这个错误,可以给我一些建议或一些链接引导我找到解决方案,我将不胜感激。
我的节点是来自 Dataframe 的列表列表,我的边缘来自元组列表。
我知道以下错误可能是由于我在 G=nx.Graph() 中涉及的节点和边, 但是如果我只需要一个静态图,它们运行良好,但是当我需要一个动态图时不知道如何修改它。
以下是我的部分代码:
...
nodelist = [A, B, C, D, E, F, G, H, N, O, P, Q, R]
#create empty graph
G = nx.Graph()
node_list = []
for sublist in nodelist:
for item in sublist:
node_list.append(item)
for i in node_list:
G.add_node(i)
G.nodes()
edges = set(AB+BC+CN+NE+ED+DO+OP+PQ+QH+HG+GF+FR+BE+HR)
# edges
G.add_edges_from(edges)
pos = nx.fruchterman_reingold_layout(G, scale=4)
#nx.draw_networkx(G, pos=pos, nodelist=nodelist[0], with_labels=False, node_size=10, node_color='#FF0000', font_size=5, width=2)
...
plot = Plot(plot_width=400, plot_height=400,x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1))
title = "track-dynamic graph"
HOVER_TOOLTIPS = [("a", "@index"), ("b", "@type")]
plot = figure(tooltips = HOVER_TOOLTIPS, tools="pan,wheel_zoom,save,reset", active_scroll='wheel_zoom',x_range=Range1d(-10.1, 10.1), y_range=Range1d(-10.1, 10.1), title=title)
network_graph = from_networkx(G, nx.spring_layout, scale=10, center=(0, 0))
network_graph.node_renderer.glyph = Circle(size=3, fill_color='blue')
network_graph.edge_renderer.glyph = MultiLine(line_alpha=0.5, line_width=1)
plot.renderers.append(network_graph)
show(plot)
上面的散景代码给我以下错误:
ValueError: 无法验证 StaticLayoutProvider(id='1058', ...).graph_layout: 需要一个 Dict(Either(String, Int), Seq(Any)) 的元素,得到 {'6600d5067598ac904ac8e1ed3d3760c0': array([- 1.67995763, 3.53275585]), 'd151da7d698cd5a6f6957ca77549ebbe': array([-5.69608212, 0.91764808])...
from_networkx
。您必须提供 layout_function
参数,它控制您想要的布局类型。您可以将任何内置的布局功能传递给networkx。 Bokeh networkx 集成用户指南 中明确演示了提供此必需参数,例如
import networkx as nx
graph = from_networkx(G, nx.spring_layout, ...) # use spring_layout
这是重现问题的帖子
在 python 3.9.14 和 bokeh 3.1.0 上有同样的问题
ValueError: failed to validate StaticLayoutProvider(id='p1076', ...).graph_layout: expected an element of Dict(Int, Seq(Any)), got {'Aemon': array([-0.31649242, 2.58870237]), 'Grenn': array([-0.74669326, 4.67785828]), 'Samwell': array([-2.08387338, 4.10110813]), 'Doran': array([4.01849557, 1.36418261]), 'Walton': array([-2.55845497, -4.56622778])}
重现问题:
附注没有足够的声誉将其作为对@bigreddot 消息的评论发布。