使用NetworkX绘制Digraph时的colormap错误
用配色栏绘制无方向的图形作品,但是Digraph的相同绘图失败了:
def plot(G):
positions=nx.circular_layout(G)
nx.get_edge_attributes(G, 'value')
edges, weights = zip(*nx.get_edge_attributes(G, 'value').items())
edge_cmap = plt.cm.Spectral.reversed()
fig, ax = plt.subplots()
ax = nx.draw_networkx_nodes(G, positions)
ax = nx.draw_networkx_labels(G, positions)
ax = nx.draw_networkx_edges(G, positions,
edgelist=edges,
connectionstyle=f'arc3, rad = 0.25',
edge_color = weights,
edge_cmap = edge_cmap,
edge_vmin=1,
edge_vmax=5)
cbar = fig.colorbar(ax, orientation='vertical')
return fig
# toy data
df = pd.DataFrame({"source": [0, 1, 2],
"target": [2, 2, 3],
"value": [3, 4, 5]})
绘制无方向的图表如预期的:
G = nx.from_pandas_edgelist(df, 'source', 'target', edge_attr='value')
plot(G)
plt.show()
绘制DigraHP返回错误:错误:
G = nx.from_pandas_edgelist(df, 'source', 'target', edge_attr='value',
create_using=nx.DiGraph)
plot(G)
plt.show()
错误:
attributeError:'list'对象没有属性'get_array'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用定向图,您的变量
ax = nx.draw_networkx_edges(...)
是箭头的列表,不是映射对象。但是,使用无向图,ax
是linecollection
,它是可映射的。请参阅更多说明 arrows = false innx.draw_networkx_edges
,以便ax ax ax
变成aaxe>代码> LineCollection
,然后将使创建配色栏成为可能。With directed graphs, your variable
ax=nx.draw_networkx_edges(...)
is a list of arrows which isn't a mappable object. However, with undirected graph,ax
is aLineCollection
which is mappable. See more explanation here.One thing you can do to make the colorbar work with directed graphs is simply add
arrows=False
innx.draw_networkx_edges
such thatax
becomes aLineCollection
, which will then in turn make creating the colorbar possible.