【问题标题】:How to deal with overlapping nodes in networkx graphics如何处理networkx图形中的重叠节点
【发布时间】:2014-05-06 05:54:39
【问题描述】:

我正在尝试在 networkx 中制作一个图形,其中节点组链接如下图:

但是,有些节点会显示出来,而其他节点则以中性色呈现。

我想知道是否有办法:

  • 为每个子组只着色一个节点(例如,一个节点为红色) - 其他节点为中性色;
  • 或为所有节点着色。

这是我的代码:

def draw_graph(transactions, method, suffix):

    G = nx.Graph()

    # get the string prefixes for each group (column of nodes)
    # possible prefixes are: aa, bb, cc, etc.
    prefixes = [c[:2] for c in classifiers]

    # for each transaction, use also a unique index (i_t)
    for i_t, t in enumerate(transactions):

        # tid is the groups signature, e.g. if the tuple of
        # classification is ('1', '2', '1') tid is 121
        # tid has always 3 characters - there are only 3
        # with at maximum 7 classifications in each group
        tid = ''.join([c[2] for c in t])

        # nodes have name the concatenated string composed of:
        # * classification (e.g. aa1) <- please note prefix
        # * tid (e.g. 121)
        # * unique identifier
        node1 = t[0] + tid + str(i_t)
        node2 = t[1] + tid + str(i_t)
        node3 = t[2] + tid + str(i_t)

        # link correspondent nodes on each group
        G.add_edge(node1, node2, weight=0.2)
        G.add_edge(node2, node3, weight=0.2)

    # based on prefix decide in which group store the node
    pos = {}
    group1 = []
    group2 = []
    group3 = []
    for node in G.nodes():
        if node.startswith(prefixes[0]):
            group1.append(node)
        elif node.startswith(prefixes[1]):
            group2.append(node)
        else:
            group3.append(node)

    # sort each group by classification AND tid
    group1.sort(key=lambda x: x[2:6])
    group2.sort(key=lambda x: x[2:6])
    group3.sort(key=lambda x: x[2:6])

    # define position for each node based on group and index
    # of node inside the group
    for i_group, group in enumerate([group1, group2, group3]):
        for i_node, node in enumerate(group):
            xpos = float(i_group) * 2
            ypos = float(i_node) / len(group)
            pos[node] = [xpos, ypos]

    # decide node color based on classification (nn[2]). 
    # avail_colors is a list of colors defined outside this method
    node_color = [avail_colors[int(nn[2])] for nn in G.nodes()]

    # draw each node with correspondent position and color
    nx.draw_networkx_nodes(G, pos, node_size=200, node_color=node_color)

    # draw edges with defined weight (not used in this example)
    for (u, v, d) in G.edges(data=True):
        w = d['weight']
        nx.draw_networkx_edges(G, pos, edgelist=[(u, v)], width=w)


    plt.axis('off')
    plt.close()

希望可以理解。如果不问我信息。

谢谢

【问题讨论】:

  • 有两种方法可以满足您的两种节点着色需求。你能发布一个小代码来展示你是如何做到这一点的吗?然后我们可以就如何修改它来做你想做的事情提出建议。
  • 抱歉.. 我忘记发了。我会尽快发布。

标签: python graphics graph nodes networkx


【解决方案1】:

这可能不是您想要的。您可以像这样控制单个节点的颜色、透明度和相对重叠;

import matplotlib.pyplot as plt
import networkx as nx

G = nx.Graph()
G.add_node(1)
G.add_node(2)
G.add_node(3)

pos = {1:(0.99,1.0), 2:(1.0,1.0), 3:(1.01,1.0)}

r = nx.draw_networkx_nodes(G, pos, nodelist=[1], node_color='r', node_size=30000)
g = nx.draw_networkx_nodes(G, pos, nodelist=[2], node_color='g', node_size=30000)
b = nx.draw_networkx_nodes(G, pos, nodelist=[3], node_color='b', node_size=30000)
r.set_zorder(3) # move red node to front
b.set_alpha(0.05) # make blue node (almost) "invisible"
plt.xlim(0.95,1.05)
plt.axis('off')
plt.show()

【讨论】:

    【解决方案2】:

    我发现,就我而言,解决我的问题的方法是设置

    linewidths=0
    

    draw_networkx_nodes(),因为是圆圈的边界产生了重叠的效果。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      • 2013-12-21
      • 2019-10-11
      • 2019-07-02
      相关资源
      最近更新 更多