在对我的original question给出了一个有用的答案之后,我决定使用内部数据(我不能在这里共享)。内部数据采用与模拟数据相同的格式。我只需将数据复制到同一个工作目录中,确保新数据具有相同的格式,即相同的列标题,等等。我使用DiffChecker来确保app.py (来自我原来的文章)与概念证明(appPOC.py)相匹配。内部数据有600多个节点,3000多个边缘。
制作交互式仪表板的代码与我用于我的original post的代码相同。然而,这一次我遇到了这个KeyError
Traceback (most recent call last):
File "appPOC.py", line 75, in <module>
hovertext = "Document Description: " + str(G.nodes[node]['Description']) + "<br>" + "Document Name: " + str(G.nodes[node]['DocName']) + "<br>" + "Document ID: " + str(G.nodes[node]['DocumentID'])
KeyError: 'Description'数据本身应该很好,因为我可以在没有节点旁边的悬停文本的情况下绘制一个网络。
总之:app.py可以绘制模拟数据,appPOC.py (它是相同的,但文件名不同)不能绘制内部数据。这使我相信,CSV文件中的内部数据有问题。
编辑:我发现如果没有在元素中列出目标,则无法绘制图形。如果(目标)节点没有在元素中定义,那么是否存在自动创建节点的问题(就像在Gephi中一样)?
发布于 2020-07-10 15:44:13
NetworkX为每个边缘的往返节点创建节点。因此,与
G = nx.from_pandas_edgelist(edges, 'Source', 'Target')你的图有所有可能的节点。然而,与
nx.set_node_attributes(G, nodes.set_index('Doc')['Description'].to_dict(), 'Description')
nx.set_node_attributes(G, nodes.set_index('Doc')['DocumentID'].to_dict(), 'DocumentID')您只对节点数据帧中的节点属性'Description'和'DocumentID'进行填充。一个简单的解决方法是替换
str(G.nodes[node]['Description'])使用
str(G.nodes[node].get('Description', ''))'DocName'和'DocumentID'也是如此。有关get方法的更多信息:Why dict.get(key) instead of dict[key]? --基本上,我们使用networkx使用dict存储值并使用get方法,该方法允许提供默认值。
一个简单的、可重复的、最小的例子
import networkx as nx
g = nx.karate_club_graph()
# all nodes in this graph have the node attribute 'club' filled
# we add a node without this node attribute
g.add_node("Test")
print(g.nodes[0]["club"])
# 'Mr. Hi'
# print(g.nodes["Test"]["club"]
# results in KeyError: 'club'
print(g.nodes["Test"].get("club", ""))
# ''https://stackoverflow.com/questions/62834929
复制相似问题