使用NetworkX绘制Digraph时的colormap错误

发布于 2025-01-30 04:30:50 字数 1345 浏览 0 评论 0 原文

用配色栏绘制无方向的图形作品,但是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'

Plotting undirected graph with a colorbar works, but the same plot for a digraph fails:

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]})

Plotting undirected graph works as expected:

G = nx.from_pandas_edgelist(df, 'source', 'target', edge_attr='value')
plot(G)  
plt.show()

Plotting a digrahp returns error:

G = nx.from_pandas_edgelist(df, 'source', 'target', edge_attr='value', 
    create_using=nx.DiGraph)
 plot(G)
plt.show()

Error:

AttributeError: 'list' object has no attribute 'get_array'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停顿的约定 2025-02-06 04:30:51

使用定向图,您的变量 ax = nx.draw_networkx_edges(...)是箭头的列表,不是映射对象。但是,使用无向图, ax linecollection ,它是可映射的。请参阅更多说明 arrows = false in nx.draw_networkx_edges ,以便 ax ax ax 变成a axe>代码> 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 a LineCollection 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 in nx.draw_networkx_edges such that ax becomes a LineCollection, which will then in turn make creating the colorbar possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文